Of course it does. You do recurcively separating each head by car and then cons them back together, what else it could do. Describe the pseudocode with examples what you are trying and by what algorithm.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
This gives more natural shared last element result, you can do rest and find how to get it return what you want (you may need accumulator parameter or reverse function):
(define (sublists lst)
(if (null? lst) '()
(append (list lst) (sublists (cdr lst)))
)
)
Lispy version 2.0
lispy> (sublists '(1 2 3 4))
((1 2 3 4) (2 3 4) (3 4) (4))
(run with Norvig's lisp written in Python )
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
If you know Prolog, you may also notice that output of my function are end parts of difference lists for original lst giving your goal answers, if you replace (1 2 3 4) answer in beginning with () answer in end.
For example
(1 2 3 4)\(2 3 4) = (1)
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
You'll need to use map or write a recursive function that is like map.
Note that your output requires O(n^2) work to construct, which means you'll need an O(n^2) algorithm here, not one of these obviously-O(n) algorithms.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177