[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Mapping by N elements

William James

5/20/2015 7:30:00 AM

Erik Naggum wrote:

> | Is there any function or macro defined by Common Lisp that does the
> | following?
> :
> | Here is its fictional documentation:
> |
> | (map-by VARLIST LIST &rest BODY)
> |
> | A macro returning a list created by binding successive N elements of LIST
> | to the symbols in VARLIST (where N is the length of VARLIST) and
> | executing BODY each time around. The return value of BODY is used as each
> | element of the return list.
>
> the much undeservedly maligned extended LOOP macro does this for me
> whenever I need the above, which is only when processing property lists:
>
> (loop
> for (key value) on <plist> by #'cddr
> collect (<whatever> key value))

Gauche Scheme:

(use srfi-1 :only (unfold))
(unfold null? (^x (apply + (take x 2))) cddr '(2 20 3 30))
===>
(22 33)


(map (pa$ apply +) (slices '(2 20 3 30) 2))
===>
(22 33)

--
We announced openings for skilled-trades trainees and invited employees to come
in and take the test.... [W]e had enough employees who made more than 85 to
fill all the openings. The highest score that any of the blacks scored on the
test was 11. The lowest score that any black made on the test was 4. All four
of those blacks went into skilled-trades training.
https://archive.org/download/TheOldmanArchives/oldman29-...
1 Answer

William James

12/1/2015 7:11:00 AM

0

WJ wrote:

> Erik Naggum wrote:
>
> > > Is there any function or macro defined by Common Lisp that does the
> > > following?
> > :
> > > Here is its fictional documentation:
> > >
> > > (map-by VARLIST LIST &rest BODY)
> > >
> > > A macro returning a list created by binding successive N elements of LIST
> > > to the symbols in VARLIST (where N is the length of VARLIST) and
> > > executing BODY each time around. The return value of BODY is used as each
> > > element of the return list.
> >
> > the much undeservedly maligned extended LOOP macro does this for me
> > whenever I need the above, which is only when processing property lists:
> >
> > (loop
> > for (key value) on <plist> by #'cddr
> > collect (<whatever> key value))

Ocaml:

let rec loop = function
k::v::rest -> k+v :: loop rest
| x -> x
in loop [2;20;3;30];;

===>
[22; 33]

--
[Y]ou can never fix a decaying empire; the empire can only fall when the time
comes. You cannot fix the empire by sending good and honest men to the capital,
because the simple logic of the system corrupts everyone along the way.
www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm