0

I'm new to scheme and I'm learning sicp.

I'm working on exercise 1.12 and tried to print a pascal triangle with the code below.

Cause I don't know much about scheme IO, I only used display procedure.

(define (pascal n k)
  (if (or (= k 1) (= k n))
    1
    (+ (pascal (- n 1) (- k 1))
       (pascal (- n 1) k))))

(define (prt_iter n row col)
  (if (< row n)
    ((display (pascal (+ row 1) (+ col 1)))
    (if (< col row)
      ((display " ")
        (prt_iter n row (+ col 1)))
      ((display "\n")
        (prt_iter n (+ row 1) 0))))
    (display "\nfinished")))

(define (prt n)
  (prt_iter n 0 0))

(prt 5)

The intepreter said "Exception: attempt to apply non-procedure #<void>".

I tried to print it manually:

(display (pascal 1 1))
(display "\n")
(display (pascal 2 1))
(display " ")
(display (pascal 2 2))
(display "\n")
; etc

and it worked.

Still I couldn't figure out what's wrong with it, especially "#<void>" confuses me a lot.

  • Parentheses are significant in Scheme: a form like `((display " ") (prt_iter ...))` doesn't evaluate the sub-forms in sequence (for that use `(begin (sub1 ...) (sub2 ...))`. `((display ...) ...)` evaluates the `display` form, producing #, so then attempts to evaluate `(# ...)`. For SICP, `#lang sicp` in Racket (using DrRacket) is often recommended. – mnemenaut Jul 11 '23 at 13:56
  • oh thk you, I also installed DrRacket and I will give it a try. – Van Young Jul 11 '23 at 14:10

0 Answers0