B. Pym
2024-06-06 10:38:54 UTC
this simple function i'm trying to write is giving me headaches!
(variations 'x '(y z)) -> ((x y z) (y x z) (y z x))
i come from a procedural programming background and find functional
programming very confusing (especially recursion). can someone give
me some hints? my attempts at this make no sense so pasting them here
would only confirm my newbish forray into LSIP. thanks for any help!
(defun variations (item list)(variations 'x '(y z)) -> ((x y z) (y x z) (y z x))
i come from a procedural programming background and find functional
programming very confusing (especially recursion). can someone give
me some hints? my attempts at this make no sense so pasting them here
would only confirm my newbish forray into LSIP. thanks for any help!
(if (null list)
(list (list item))
(cons (cons item list)
(mapcar (lambda (rest) (cons (car list) rest))
(variations item (cdr list))))))
(use srfi-1) ;; split-at
(use srfi-42) ;; list-ec
(define (variations x lst)
(list-ec (: i (+ 1 (length lst)))
(receive (a b) (split-at lst i)
`(,@a ,x ,@b))))
(variations '- '(a b c))
===>
((- a b c) (a - b c) (a b - c) (a b c -))
(variations '- '(a))
===>
((- a) (a -))
(variations '- '())
===>
((-))