Note: this is a bonus for homework, but I have spent way too long on trying things to no avail. Help is much appreciated, but not necessary I suppose.
Premise:
generate a powerset for a list of numbers, but without using any helpers, explicit recursion, looping, or functions/constants other than cons
, first
, rest
, empty?
, empty
, else
, lambda
, and cond
, while using only one define
on the language level Intermediate Student with Lambda
. The order of the powerset does not matter.
What I've tried so far:
I have discovered the Y-combinator and anonymous recursion thanks to this post (author has the same end goal but we have different approaches, so the information in his post does not solve my problem), and the powerset
code in this answer, and with that I have written the following:
(define (powerset aL)
(((lambda (X)
((lambda (proc)
(proc proc))
(lambda (proc)
(X (lambda (arg)
((proc proc) arg))))))
(lambda (subset)
(lambda (lst)
(cond
[(empty? lst) (list empty)]
[else (combine (first aL) (powerset (rest aL)))])))) aL)
(define (combine a r)
(cond
[(empty? r) empty]
[else (cons (cons a (first r)) (cons (first r) (combine a (rest r))))]))
I'm testing this code by running:
(check-expect (powerset '(1 2 3))
(list '(1 2 3) '(2 3) '(1 3) '(3) '(1 2) '(2) '(1) '()))
This code runs and produces a correct result, but, as you can see, I still rely on an external helper function, combine
, and I have no idea how to convert that into a lambda
since to my knowledge, the Y-combinator only works with one parameter and combine
needs 2. Perhaps my logic or approach to this problem is flawed. I have limited experience with lambda
so I could be missing knowledge as well.
What I need help with: any suggestions as to next steps, helping me intergrate combine
into powerset
, providing hints/clues to correct logic/approach, or a solution would be much appreciated.
Thanks in advance!