1

I'm using chezscheme 9.5.8. I created the following function, which receives a string and prints a phrase using that string:

> (define (ijustlove str)
    (display
      (string-append "I just love "
                     (string-upcase str)
                     "!!!\n")))
> (ijustlove "bananas")
I just love BANANAS!!!

But, if I map the function over a list, it prints #<void> (Which is the same as doing (display (void)) ):

> (map ijustlove '("strawberries" "bananas" "grapes"))
I just love STRAWBERRIES!!!
I just love BANANAS!!!
I just love GRAPES!!!
(#<void> #<void> #<void>)

Is there any way to avoid this?

  • 1
    `display` prints the string, and returns `#`. `map` creates a list of all the returned values. – Barmar Aug 27 '22 at 16:29
  • 1
    Use `for-each` if you don't need the list of results. – Barmar Aug 27 '22 at 16:31
  • 1
    When a procedure returns `#` to the REPL, that result isn't printed. Compare `(display "hi")` and `(list (display "hi"))`. – molbdnilo Aug 28 '22 at 09:28
  • The better way to do this is to split up logic that does something and the side effect. eg. `ijustlove` should perhaps just be the string-append part and *not* display. Then `(map ijustlove '("lisp" "haskell"))` would evaluate to `("I just love LISP!!!" "I just love HASKELL!!!)` and if you wanted to print them you did `(for-each display (map ijustlove '("lisp" "haskell")))` or `(for-each (λ (v) (display (ijustlove v))) '("lisp" "haskell"))` – Sylwester Aug 29 '22 at 14:56

0 Answers0