A function I’m supposed to build is supposed to take in a list of numbers as a parameter, and give a single function as an output that does as follows: If the number in the list is a positive number, add it, if it is negative multiply by it, and if it is 0, square the number.
For example, if I pass in (4 -1 0), it should return a function that takes in a single parameter, adds 4 to it, multiplies it by -1, squares it, and returns that.
I think I’m on the right track, but I’m getting seriously confused at this point. I’m not necessarily looking for a solution, but any help getting there would be amazing. Here’s what I have so far:
(define (buildseries L)
(define (a x)
(lambda (y) (+ x y)))
(define (m x)
(lambda (y) (* x y)))
(define (s x)
(lambda (x) (* x x)))
(define (funcfind d)
(cond
[(null? d) (a 0)]
[(> d 0) (a d)]
[(= d 0) (s d)]
[(< d 0) (m d)]))
(funcfind (first L)))
((buildseries '(2)) 2)
I don’t know how to build a function that is a composite of other functions… Just feeling lost here.
Jon’s answer is very good. You should try to implement it as much as you can. If you need to, you can also refer to my answer here (which doesn’t follow Jon’s approach since I wrote most of my answer before he posted his):
You should study it to see how it works, then write your own version using a completely different approach, like Jon’s idea about using
compose. 🙂Edit: The function I wrote can be simplified further: using SRFI 1’s
fold, you can do this: