Search code examples
schemesicpguile

SICP practise 3.51 Wrong type to apply: #<syntax-transformer cons-stream>


In practice 3.51 of the SICP, it defines a procedure "show", and use stream-map to create a stream:

(add-to-load-path ".")
(load "stream.scm")

(define (show x)
  (display-line x)
  x)

(define x0 (stream-enumerate-interval 0 3))

(display-stream x0)   ;succ, no error

(stream-map show x0)  ;all element printed, but interpreter report error at last

The other staff about streams in stream.scm:

#!/usr/bin/guile
!#

(define (stream-null? s)
  (null? s))

(define (stream-ref s n)
  (if (= n 0)
      (stream-car s)
      (stream-ref (stream-cdr s) (- n 1))))

(define (stream-map proc s)
  (if (stream-null? s)
      the-empty-stream
      (cons-stream (proc (car s))
                   (stream-map proc (stream-cdr s)))))

(define (stream-for-each proc s)
  (if (stream-null? s)
      'done
      (begin
        (proc (stream-car s))
        (stream-for-each proc (stream-cdr s)))))

(define (display-stream s)
  (stream-for-each display-line s))

(define (display-line x)
  (display x))

(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))

(define (stream-enumerate-interval low high)
  (if (> low high)
      the-empty-stream
      (cons-stream
        low
        (stream-enumerate-interval (+ low 1) high))))

(define (stream-filter pred stream)
  (cond
    ((stream-null? stream) the-empty-stream)
    ((pred (stream-car stream))
     (cons-stream (stream-car stream)
                  (stream-filter pred (stream-cdr stream))))
    (else
      (stream-filter pred (stream-cdr stream)))))

(define-syntax cons-stream
  (syntax-rules ()
    ((_ a b) (cons a (delay b)))))

(define the-empty-stream '())

(define (stream-enumerate-interval low high)
  (if (> low high)
      the-empty-stream
      (cons-stream low
                   (stream-enumerate-interval (+ low 1) high))))

The error is like this:

0123Backtrace:
           8 (apply-smob/1 #<catch-closure 557e16ae8c20>)
In ice-9/boot-9.scm:
    705:2  7 (call-with-prompt ("prompt") #<procedure 557e16aef6a0 …> …)
In ice-9/eval.scm:
    619:8  6 (_ #(#(#<directory (guile-user) 557e16b9e140>)))
In ice-9/boot-9.scm:
   2312:4  5 (save-module-excursion #<procedure 557e16b24330 at ice-…>)
  3822:12  4 (_)
In stream.scm:
     25:8  3 (stream-map #<procedure show (x)> (0 . #<promise (1 . …>))
     25:8  2 (stream-map #<procedure show (x)> (1 . #<promise (2 . …>))
     25:8  1 (stream-map #<procedure show (x)> (2 . #<promise (3 . …>))
In unknown file:
           0 (_ 3 ())

I've no idea why display-stream succeed, but stream-map "show" is fail. The code is the same as the sample in SICP. The scheme interpreter is 'guile'.

Any ideas? THX


Solution

  • The error disappeared when I moved

    (define-syntax cons-stream
      (syntax-rules ()
        ((_ a b) (cons a (delay b)))))
    

    to the top of the file.

    Apparently, in Guile it must be defined above its first use point in the file.

    You didn't see the error with stream-enumerate-interval because it is defined twice - the last time below the definition of cons-stream.

    Tested in https://ideone.com which uses "guile 2.0.13".