this simple function i'm trying to write is giving me headaches!
basically i want to do something that does:
(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)
(if (null list)
(list (list item))
(cons (cons item list)
(mapcar (lambda (rest) (cons (car list) rest))
(variations item (cdr list))))))
(defun variations (x list)
(loop for cons on (cons nil list) collecting
(nconc (ldiff list (cdr cons)) (cons x (cdr cons)))))
Okay, so that's arguably obfuscated Lisp. But you should never pass up
a chance to combine LOOP, LDIFF, and abuse of Common Lisp's Lisp-2
nature.
this simple function i'm trying to write is giving me headaches!
basically i want to do something that does:
(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)
(if (null list)
(list (list item))
(cons (cons item list)
(mapcar (lambda (rest) (cons (car list) rest))
(variations item (cdr list))))))
Pascal Bourguignon wrote:
this simple function i'm trying to write is giving me headaches!here
basically i want to do something that does:
(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
would only confirm my newbish forray into LSIP. thanks for any help!
(defun variations (item list)
(if (null list)
(list (list item))
(cons (cons item list)
(mapcar (lambda (rest) (cons (car list) rest))
(variations item (cdr list))))))
Gauche Scheme:
(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 '- '()) ===> ((-))
Pascal Bourguignon wrote:
this simple function i'm trying to write is giving me headaches! basically i want to do something that does:
(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)
(if (null list)
(list (list item))
(cons (cons item list)
(mapcar (lambda (rest) (cons (car list) rest))
(variations item (cdr list))))))
Gauche Scheme:
(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 '- '())
===>
((-))
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 328 |
Nodes: | 16 (2 / 14) |
Uptime: | 36:48:23 |
Calls: | 7,297 |
Calls today: | 4 |
Files: | 12,605 |
Messages: | 5,572,378 |