-1

I want my procedure to print something, then return that something. I tried this

(define (print x)
    ((display x)
     x))

Shouldn't it be as straightforward to say that this procedure displays x at first then returns it as it's expressed at the end of the procedure? Well, apparently it is wrong, and that there is something very fundamental I don't understand about Scheme. So anybody, help me understand this. Thanks

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Snusifer
  • 485
  • 3
  • 17

1 Answers1

6

The posted code has too many parentheses. In Lisp, parentheses have meaning, express function call, not just grouping of operands.

With ((display x) x), your code tries to call as a function the (unspecified in R6RS Scheme) value returned by the call to (display x), with the value of x as the argument in that function call. Instead:

(define (print x)
  (display x)
  x)

works.

Just remove the extraneous parentheses. They are not harmless.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
ad absurdum
  • 19,498
  • 5
  • 37
  • 60