[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Idiom for gathering pairs from a list?

William James

11/7/2015 4:24:00 PM

Rob Warnock wrote:

> The problem with any such automatic sizing is LOOPs like this:
>
> (loop for (a b c . rest) on list by #'cdddr ...)
>
> or even this:
>
> (flet ((stepper (list)
> (case (car list)
> ((:one) (cddr list))
> ((:two) (cdddr list))
> ((:three) (cdddr list))
> (otherwise (cdr list)))))
> (loop for (key a b c) on list by #'stepper ...))
>
> which can parse lists like this:
>
> (:one 123 :two 234 453 :three 7 5 8 :special :other 99 22 :two 34 54)


Let's test that crap.

(setq mlist
'(:one 123 :two 234 453 :three 7 5 8 :special :other 99 22 :two 34 54))
(flet ((stepper (list)
(case (car list)
((:one) (cddr list))
((:two) (cdddr list))
((:three) (cdddr list))
(otherwise (cdr list)))))
(loop for (key a b c) on mlist by #'stepper
do (print (list key a b c))))

(:ONE 123 :TWO 234)
(:TWO 234 453 :THREE)
(:THREE 7 5 8)
(8 :SPECIAL :OTHER 99)
(:SPECIAL :OTHER 99 22)
(:OTHER 99 22 :TWO)
(99 22 :TWO 34)
(22 :TWO 34 54)
(:TWO 34 54 NIL)


MatzLisp (Ruby):

[:one,123,:two,234,453,:three,7,5,8,:special,:other,99,22,:two,34,54].
slice_before{|x| x.is_a? Symbol}.to_a

==>[[:one, 123],
[:two, 234, 453],
[:three, 7, 5, 8],
[:special],
[:other, 99, 22],
[:two, 34, 54]]

--
Let us be brought to believe it is ... favorable to ... our interest to
transfer the African to his native clime, and we shall find a way to do it,
however great the task may be. --- A. Lincoln, Sept. 22, 1856