Search code examples
functionsyntaxschemedefinitionfunction-call

Procedure to display and return something in scheme?


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


Solution

  • 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.