Discussion:
.Re: simple lisp function
(too old to reply)
B. Pym
2024-06-06 10:38:54 UTC
Permalink
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)
(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 '- '())
===>
((-))
HenHanna
2024-06-06 20:21:06 UTC
Permalink
Post by B. Pym
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)
(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)
(variations '- '(a b c)) ===> ((- a b c) (a - b c) (a b - c) (a b
c -))
(variations '- '(a)) ===> ((- a) (a -))
(variations '- '()) ===> ((-))
I remember writing this in Lisp and Python. a few years ago.


Is Scheme (or Gauche) used outside of the Academia?

What kind of people (other than Grad students, Researchers in
Prog.Lang design)
would know about Split-at and List-ec and Receive
?
steve g
2024-08-10 17:33:15 UTC
Permalink
< > 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 '- '()) ===> ((-))
Post by HenHanna
I remember writing this in Lisp and Python. a few years ago.
Is Scheme (or Gauche) used outside of the Academia? What kind of people
(other than Grad students, Researchers in
Prog.Lang design)
would know about Split-at and List-ec and Receive ?
lisp programmers?

CL-USER> (mapcon #'list '(X 1 2 3 4))
((X 1 2 3 4) (1 2 3 4) (2 3 4) (3 4) (4))

B. Pym
2024-06-06 23:31:05 UTC
Permalink
Post by B. Pym
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)
(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)
(variations '- '(a b c))
===>
((- a b c) (a - b c) (a b - c) (a b c -))
(variations '- '(a))
===>
((- a) (a -))
(variations '- '())
===>
((-))
Gauche or Racket Scheme:

(use srfi-42) ;; list-ec for Gauche
or
(require srfi/42) ;; list-ec for Racket

(define (variations item seq)
(list-ec (: i (+ 1 (length seq)))
`(,@(take seq i) ,item ,@(drop seq i))))
Loading...