Search code examples
dictionaryschemevoidr5rs

Using map in scheme, a strange thing happens when I use the display function


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?


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>)
    >