[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Self Study Question (mapcar) NOT HOMEWORK!

William James

5/9/2015 11:01:00 AM

Barry Margolin wrote:

> If you'll recall, in my answer to the initial post in this thread (where
> the exercise was to implement this using MAPCAR), I said that in real life
> this would probably be implemented using DO or LOOP. Then someone asked
> for a recursive solution. Both the MAPCAR and recursive functions are
> interesting academic problems, but they aren't necessarily the most natural
> solution for this particular problem.
>
> A simple, easy-to-understand solution, IMHO, is:
>
> (defun pos+ (list)
> (loop for pos upfrom 0
> for element in list
> collect (+ pos element)))
>
> Crystal clear (even if you don't "like" LOOP, you should have little
> trouble understanding it), and about as efficient as possible.

Gauche Scheme:

(define (pos+ list)
(map + list (lrange 0)))

(pos+ '(200 300 400 500))
===>
(200 301 402 503)


Another way:

(use gauche.sequence :only (map-with-index))
(define (pos+ list)
(map-with-index + list))


--
The conclusion of this analysis is that democracy is identified not with the
power of the people to pursue their perceived interests. Rather, democracy is
conceptualized as guaranteeing that majorities will not resist the expansion of
power of minorities even if that means a decline in their own power.
--- Kevin MacDonald; "The Frankfurt School of Social Research and the
Pathologization of Gentile Group Allegiances"
6 Answers

Jeff Barnett

5/9/2015 4:49:00 PM

0

WJ wrote on 5/9/2015 5:01 AM:
> Barry Margolin wrote:
>
>> If you'll recall, in my answer to the initial post in this thread (where
>> the exercise was to implement this using MAPCAR), I said that in real life
>> this would probably be implemented using DO or LOOP. Then someone asked
>> for a recursive solution. Both the MAPCAR and recursive functions are
>> interesting academic problems, but they aren't necessarily the most natural
>> solution for this particular problem.
>>
>> A simple, easy-to-understand solution, IMHO, is:
>>
>> (defun pos+ (list)
>> (loop for pos upfrom 0
>> for element in list
>> collect (+ pos element)))
>>
>> Crystal clear (even if you don't "like" LOOP, you should have little
>> trouble understanding it), and about as efficient as possible.
>
> Gauche Scheme:
>
> (define (pos+ list)
> (map + list (lrange 0)))
>
> (pos+ '(200 300 400 500))
> ===>
> (200 301 402 503)
>
>
> Another way:
>
> (use gauche.sequence :only (map-with-index))
> (define (pos+ list)
> (map-with-index + list))

Let's repeat what BM said: Crystal clear (even if you don't "like" LOOP,
you should have little trouble understanding it), and about as efficient
as possible.

How about an example from you that is crystal clear? Since that was the
post you replies to, reply to it.

It's nice that you want to share your homework in many languages with us
but at least stick to the theme of the lesson. Please.
--
Jeff Barnett

William James

5/9/2015 5:50:00 PM

0

> > (defun pos+ (list)
> > (loop for pos upfrom 0
> > for element in list
> > collect (+ pos element)))
> >
> > Crystal clear

No, it isn't. Is the looping nested, or is it in parallel?

Gauche Scheme:

(use srfi-42)

;; nested
gosh> (do-ec (: c '(a b)) (: n 3) (print c n))
a0
a1
a2
b0
b1
b2

;; parallel
gosh> (do-ec (:parallel (: c '(a b)) (: n 3)) (print c n))
a0
b1

Barry Margolin

5/10/2015 12:26:00 AM

0

In article <mildo2$oht$1@dont-email.me>, Jeff Barnett <jbb@notatt.com>
wrote:

> It's nice that you want to share your homework in many languages with us
> but at least stick to the theme of the lesson. Please.

Just ignore WJ, like a sane person.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Marco Antoniotti

5/10/2015 7:15:00 PM

0

On Saturday, May 9, 2015 at 6:49:11 PM UTC+2, Jeff Barnett wrote:
> WJ wrote on 5/9/2015 5:01 AM:
....
> Let's repeat what BM said: Crystal clear (even if you don't "like" LOOP,
> you should have little trouble understanding it), and about as efficient
> as possible.
>
> How about an example from you that is crystal clear? Since that was the
> post you replies to, reply to it.
>
> It's nice that you want to share your homework in many languages with us
> but at least stick to the theme of the lesson. Please.
> --
> Jeff Barnett

Is it troll feeding season already :) :) :)

Cheers
--
MA

William James

12/1/2015 6:29:00 PM

0

WJ wrote:

> Barry Margolin wrote:
>
> > If you'll recall, in my answer to the initial post in this thread (where
> > the exercise was to implement this using MAPCAR), I said that in real life
> > this would probably be implemented using DO or LOOP. Then someone asked
> > for a recursive solution. Both the MAPCAR and recursive functions are
> > interesting academic problems, but they aren't necessarily the most natural
> > solution for this particular problem.
> >
> > A simple, easy-to-understand solution, IMHO, is:
> >
> > (defun pos+ (list)
> > (loop for pos upfrom 0
> > for element in list
> > collect (+ pos element)))

Ocaml:

Using map:

List.mapi (+) [200;300;400;500];;
===>
[200; 301; 402; 503]

Using recursion:

let rec go i = function
[] -> []
| x::xs -> x+i :: go (i+1) xs
in go 0 [200;300;400;500];;
===>
[200; 301; 402; 503]

--
"It is not as wrong raping a Swedish girl as raping an Arab girl," says Hamid,
in an interview about a gang rape involving a Swedish girl and immigrant
perps.... [T]his massive spike in rapes is caused by mass immigration ... that
.... international organizations want to continue ... until the native
population has been reduced to a persecuted minority.
-- amren.com/news/2009/09/sweden_tops_eur/

William James

1/20/2016 11:07:00 AM

0

WJ wrote:

> Barry Margolin wrote:
>
> > If you'll recall, in my answer to the initial post in this thread (where
> > the exercise was to implement this using MAPCAR), I said that in real life
> > this would probably be implemented using DO or LOOP. Then someone asked
> > for a recursive solution. Both the MAPCAR and recursive functions are
> > interesting academic problems, but they aren't necessarily the most natural
> > solution for this particular problem.
> >
> > A simple, easy-to-understand solution, IMHO, is:
> >
> > (defun pos+ (list)
> > (loop for pos upfrom 0
> > for element in list
> > collect (+ pos element)))
> >
> > Crystal clear (even if you don't "like" LOOP, you should have little
> > trouble understanding it), and about as efficient as possible.

I see two FORs. Does that mean that there are two nested loops?

MatzLisp (Ruby):

[300,400,500,600].map.with_index &:+
==>[300, 401, 502, 603]

Therefore, map is the most natural solution in a Lispy language.

--
Elie [Wiesel] thus could have remained at Birkenau to await the Russians.
Although his father had permission to stay with him as a hospital patient or
orderly, father and son talked it over and decided to move out with the
Germans. --- Robert Faurisson