Question

in scheme (I'm using racket R5RS) when I call this procedure

(map display '(1 2 3 4 5))

it returns

12345(#<void> #<void> #<void> #<void> #<void>)

Why is that? What's up with the void?

Was it helpful?

Solution

You said:

"it returns 12345(#<void> #<void> #<void> #<void> #<void>)"

which isn't precisely correct. That is what it prints/displays; not what it returns. You get the return value with:

> (define test (map display '(1 2 3 4 5)))
12345> test
(#<void> #<void> #<void> #<void> #<void>)
> 

Here it is clearer: '12345' was printed; the return value, bound to test, is your 'void' list.

The reason for the 'void' values is that map applies its function to each element in the list and constructs a new list with the value returned by function. For display the return value is 'void'. As such:

> (define test2 (display 1))
1> (list test2)
(#<void>)
> 

OTHER TIPS

map collects the results of each invocation of the function on each element of the list and returns the list of these results.

display returns an unspecified value. #<void> is just something that Racket is using. It could have returned 42 as well.

You probably have a typo. It should have returned

12345(#<void> #<void> #<void> #<void> #<void>)

without the leading open parenthesis. I.e. the five values are isplayed, and then the return value.

You need to use for-each instead of map, in your case. for-each is non-collecting, unlike map.

Also, for-each is guaranteed to pass the list items from left to right to your function. map makes no such guarantee, and is allowed to pass the list items in any order. (Although, in the specific case of Racket, map does use a left-to-right order. But you cannot depend on this in other Scheme implementations; some use a right-to-left order, and theoretically, other orderings are possible.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top