[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: newbie question

William James

9/30/2015 8:15:00 AM

Giorgos Keramidas wrote:

> > say there's a list of even length
> > (a b c d e f ......)
> >
> > how do i turn this into
> > ((a b) (c d) (e f)....)
> >
> > simply how do i couple the elements?
>
> A bit of experimentation with (loop) seems easy:
>
> CL-USER(5): (loop for (a b) on '(1 2 3 4)
> collect (list a b))
> ((1 2) (2 3) (3 4) (4 NIL))
>
> Almost there. We only need the pairs starting from 'even' zero-based
> indexes. This is fairly straightforward to write using loop again:
>
> CL-USER(6): (loop for (a b) on '(1 2 3 4)
> for index from 0
> when (evenp index)
> collect (list a b))
> ((1 2) (3 4))

Gauche Scheme:

(use srfi-1 :only (unfold))
(unfold null? (cut take <> 2) cddr (iota 8))
===>
((0 1) (2 3) (4 5) (6 7))

Another way:

(let f ((xs (iota 8)))
(if (null? xs) () (cons (take xs 2) (f (cddr xs)))))

The easy way:

(slices (iota 8) 2)

--
As Helle "Hamas" Klein, political editor of Sweden's largest newspaper
Aftonbladet, boasts: "If the debate is going to be about whether there are
problems with immigrants, we don't want it". Welcome to Sweden, the country
where the media doesn't even pretend to champion freedom of speech, but openly
brags about censorship.
jordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html