[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: good lisp style ?

William James

2/18/2016 4:30:00 PM

Nils Goesche wrote:

> > I've written the following function to find out all pairs in a
> > list:
> >
> > (defun find-all-pairs (lst)
> > "Finds all pairs - unique subsets of size 2 in the given list"
> > (cond
> > ((null lst) '())
> > (t (append (mapcar #'(lambda (x) (list (car lst) x)) (cdr lst))
> > (find-all-pairs (cdr lst))))))
> >
> > Although it is functional and short, I'm concerned about the usage
> > of mapcar in the recursion with append.
>
> You can safely use NCONC instead of APPEND. You can also easily avoid
> recursion as in
>
> (defun find-all-pairs (list)
> (mapcon (lambda (tail)
> (mapcar (lambda (x)
> (list (car tail) x))
> (cdr tail)))
> list))

MatzLisp (Ruby):

[:a,:b,:c,:d].combination(2).to_a
==>[[:a, :b], [:a, :c], [:a, :d], [:b, :c], [:b, :d], [:c, :d]]


--
[T]he number of Jews killed there was 800,000,000.... [T]he Talmud makes it all
clear by informing us that the blood of the holocausted Jews ran to the sea in
a huge tidal wave that swept boulders in its path and was so deep that it
reached the nostrils of the Romans' horses.
nationalvanguard.org/2014/09/disillusioned-part-1/