• #### .Re: simple lisp function

From Robert L.@21:1/5 to Pascal Bourguignon on Tue Feb 22 08:43:39 2022
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))))))

A monkey (Peter Seibel <peter@javamonkey.com>) wrote:

(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.

By using a Lisp instead of CL (COBOL-Like), we can make it shorter.

Gauche Scheme or Racket:

(require srfi/1) ;; iota for Racket

(define (variations x seq)
(map (lambda (i) `(,@(take seq i) ,x ,@(drop seq i)))
(iota (+ 1 (length seq)))))

(variations '- '(a b c))
===>
((- a b c) (a - b c) (a b - c) (a b c -))

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From B. Pym@21:1/5 to Pascal Bourguignon on Thu Jun 6 10:38:54 2024
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 '- '())
===>
((-))

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to B. Pym on Thu Jun 6 20:21:06 2024
B. Pym wrote:

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 '- '()) ===> ((-))

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
?

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From B. Pym@21:1/5 to B. Pym on Thu Jun 6 23:31:05 2024
On 6/6/2024, B. Pym wrote:

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 '- '())
===>
((-))

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))))

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)