Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: my first Lisp code...comments welcome

10 views
Skip to first unread message

WJ

unread,
Aug 30, 2015, 3:01:48 PM8/30/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Gareth McCaughan wrote:

> (defun compositions (n k)
> "Return a list containing all K-element lists of non-negative
> integers with sum N. Caller guarantees that N,K are non-negative
> integers."
> (if (zerop k)
> (if (zerop n) (list nil) nil)
> (loop for first-item upfrom 0 to n nconc
> (mapcar (lambda (tail) (cons first-item tail))
> (compositions (- n first-item) (1- k))))))


Gauche Scheme:

(use srfi-42 :only (append-ec))

(define (compositions n k)
(if (zero? k)
(if (zero? n) '(()) '())
(append-ec (:range first-item (+ 1 n))
(map (pa$ cons first-item) (compositions (- n first-item) (- k 1))))))

gosh> (compositions 4 3)
((0 0 4) (0 1 3) (0 2 2) (0 3 1) (0 4 0) (1 0 3) (1 1 2) (1 2 1) (1 3 0)
(2 0 2) (2 1 1) (2 2 0) (3 0 1) (3 1 0) (4 0 0))

--
An important part of the explanation is the role of mass media in Sweden. Not a
single TV-program, radio program, or big newspaper would give space to critics
of the multicultural project.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
0 new messages