Newbie: using map, apply, for-each
Author Message
Newbie: using map, apply, for-each

Howdy,

I'm sorry if this is a frequently asked question, but I've been
searching the web and have not been able to find an answer so far.  I
have been trying to learn some Scheme on my own and I was doing okay
until I came across map, apply and for-each.

Here is some code that might summarize my question:

(define first -5)
(define second 8)
(abs first)
(abs second)
(map abs '(-5 8))
(map abs '(first second))

The responses that I get from DrScheme are:

(abs first) returns 5 -- which I expected
(abs second) returns 8
(map abs '(-5 8)) -- returns (5 8)

However, (map abs '(first second)) returns:

abs: expects argument of type <real number>; given first

So, it looks like the variables that I'm passing to map are not being
resolved.  I have experimented with the 'values' function -- but
without much luck.

Can anyone point me in the right direction?  I'm sure that I'm missing
something obvious about how to get this to work correctly.

Thank you very much!

Sent via Deja.com http://www.*-*-*.com/

Sat, 19 Apr 2003 05:01:50 GMT
Newbie: using map, apply, for-each

Quote:
> Howdy,

> I'm sorry if this is a frequently asked question, but I've been
> searching the web and have not been able to find an answer so far.  I
> have been trying to learn some Scheme on my own and I was doing okay
> until I came across map, apply and for-each.

> Here is some code that might summarize my question:

> (define first -5)
> (define second 8)
> (abs first)
> (abs second)
> (map abs '(-5 8))
> (map abs '(first second))

> The responses that I get from DrScheme are:

> (abs first) returns 5 -- which I expected
> (abs second) returns 8
> (map abs '(-5 8)) -- returns (5 8)

> However, (map abs '(first second)) returns:

> abs: expects argument of type <real number>; given first

> So, it looks like the variables that I'm passing to map are not being
> resolved.  I have experimented with the 'values' function -- but
> without much luck.

> Can anyone point me in the right direction?  I'm sure that I'm missing
> something obvious about how to get this to work correctly.

> Thank you very much!

Hello,

A clue for you is this:
evaluate
'(first second)
what do you get?  Is that what you expected?
now evaluate
(list first second)

Cheers,
John

--
[:o|]

Sent via Deja.com http://www.deja.com/

Sat, 19 Apr 2003 05:17:33 GMT
Newbie: using map, apply, for-each

Quote:

>(define first -5)
>(define second 8)
>(abs first)
>(abs second)
>(map abs '(-5 8))
>(map abs '(first second))
[...]
>(map abs '(-5 8)) -- returns (5 8)

>However, (map abs '(first second)) returns:

>abs: expects argument of type <real number>; given first

>So, it looks like the variables that I'm passing to map are not being
>resolved.  I have experimented with the 'values' function -- but
>without much luck.

Actually, the problem is not from map; when you call

(map abs '(first second))

you are not passing the variables first and second to map.  More directly:

> 'first
first
> first
-5
> '(first second)
(first second)
> '(-5 8)
(-5 8)

You need to create a list containing the values of first and second, but the
quote prevents those variables from being evaluated.  Two other alternatives:

> (list first second)
(-5 8)
> (cons first (cons second '()))
(-5 8)

best,
jmj
--

( If I were a bug, I would want to be a true Renaissance bug. )

Sat, 19 Apr 2003 05:34:50 GMT
Newbie: using map, apply, for-each
John & Jordan,

Thanks gentlemen!!  I should have been able to figure this out on my
own. :>(  Sorry for wasting the bandwidth. . .

I have been working my way (ever so slowly) through "The Scheme
Programming Language" by Kent Dybvig.  It seems to be a very good book,
but I'm having a hard time learning the "Scheme way of thinking."

I've been falling the ng for a little while now, reading it when I
can.  I've been really impressed by the Scheme community and all of the
ambitious projects that are underway.

Thanks again for all of your assistance!

Cheers,

Alekum

Sent via Deja.com http://www.deja.com/

Sat, 19 Apr 2003 07:57:47 GMT
Newbie: using map, apply, for-each

Quote:

>Howdy,

>I'm sorry if this is a frequently asked question, but I've been
>searching the web and have not been able to find an answer so far.  I
>have been trying to learn some Scheme on my own and I was doing okay
>until I came across map, apply and for-each.

>Here is some code that might summarize my question:

>(define first -5)
>(define second 8)
>(abs first)
>(abs second)
>(map abs '(-5 8))
>(map abs '(first second))

>The responses that I get from DrScheme are:

>(abs first) returns 5 -- which I expected
>(abs second) returns 8
>(map abs '(-5 8)) -- returns (5 8)

>However, (map abs '(first second)) returns:

>abs: expects argument of type <real number>; given first

>So, it looks like the variables that I'm passing to map are not being
>resolved.  I have experimented with the 'values' function -- but
>without much luck.

they aren't because the quote in '(first second) prevents them from
being evaluated.  you want to pass a list with those two value.  you
can do it easily with the list function: (list first second)

hs

Sat, 19 Apr 2003 11:26:27 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages