Note: This is kind of homework, kind of not -- the end goal is to have a function that produces a powerset of a set of numbers supplied to the function as a list of numbers. I have a recursive version of the function but I now need to find some ways of replacing each explicitly recursive function in the solution I have (append
, mapm
etc.) with an equivalent lambda-only expression.
As such, I am starting with smaller problems and hope to combine them all to write a full function. I've managed to come up with a non-recursive factorial function using pure-lambda (Y combinator) but I am now trying to come up with a nice function that squares every number in a list -- trying to solve smaller problems before jumping up to a multiply-recursive function:
(define (sqrlist numlist)
(((lambda (f)
((lambda (x) (x x))
(lambda (g)
(f (lambda (x) ((g g) x))))))
(lambda (f)
(lambda (x)
(cons (sqr (first x)) (rest x))))) numlist))
The code above doesn't recurse, despite the presence of the Y combinator before it -- I'm obviously having some issues passing the proper parameters to the functions within -- any ideas?
Here's a possible answer, I know that you solved it already, but it could be useful to have a second opinion :)
It's "lambda-only" since it's written exclusively in terms of anonymous functions, it doesn't even use
define
. Here's how to call it: