this is the original form:
(define (split-by l p k)
(let loop ((low '())
(high '())
(l l))
(cond ((null? l)
(k low high))
((p (car l))
(loop low (cons (car l) high) (cdr l)))
(else
(loop (cons (car l) low) high (cdr l))))))
and i'm trying to convert let, this is what I have tried:
(define (split-by l p k)
(lambda (loop)
(cond ((null? l) (k low high))
((p (car l))
(loop low (cons (car l) high) (cdr l)))
(else
(loop (cons (car l) low) high (cdr l))
((low '()) (high '()) (l l))))))
I don't know how to fix this so if anyone could help what I am doing wrong would be a great help.
If I understand correctly what you're doing,
pis a predicate and you split the listlaccording to this, aggregating your two resulting lists with the aggregation functionk; in pseudo-code:The problem when replacing your
letis that theloopfunction is recursively defined. It is of the form:You absolutely can use this directly in your function, defining the 'inner' recursive part, but this cannot be made using simple
lambdawithoutlet: the function needs to refer to itself (since it is recursive), and you can only do that by assigning it a name.definewill do that,letwill let you do that, but no matter how you turn it, you need that self-reference. If you're clever and pass in a continuation:You've removed that self-reference by making it explicit, but what do you pass as
cont? Well, if you assigned that via a let, you have a symbol refering to it:Or more concisely with a
define, which does the exact same thing (without the need to pass in the continuation):All of them operate similarly:
But you cannot get away with defining a symbol for your recursive function*.
As to why your example didn't work, it's working perfectly fine, except that it creates a function, taking as argument a function (which I called
contabove) and applying your logic given that functionloop. Since you then don't have any 'loop' to pass it (as you haven't bound it), it returns that function and proceeds to do nothing (in addition, in your returnedlambda,lowandhighare not defined).* This is not entirely true as you could do it using combinators on your lambda, but that would make it much more complex than it ought to:
Or for an even
uglierpurer version, with an inline combinator:Which work as expected (thanks to the layers of lambdas):