2011-06-07 42 views
2

你可以看到什麼是錯的:定義使用圖形繪製線的過程

(define (box d x1 y1 x2 y2) (
           (graphics-draw-line d x1 y1 x1 y2) 
           (graphics-draw-line d x1 y2 x2 y2) 
           (graphics-draw-line d x2 y2 x2 y1) 
           (graphics-draw-line d x2 y1 x1 y1))) 

當我這樣稱呼它:

(begin 
    (define w (make-graphics-device 'x)) 
    (box w .10 .10 .20 .20)) 

我得到一個errer:

;The object #!unspecific is not applicable. 
;To continue, call RESTART with an option number: 
; (RESTART 2) => Specify a procedure to use in its place. 
; (RESTART 1) => Return to read-eval-print level 1. 

2 error> 

此作品:

(begin 
    (define w (make-graphics-device 'x)) 
    (graphics-draw-line w .1 .1 .1 .2) 
    (graphics-draw-line w .1 .2 .2 .2) 
    (graphics-draw-line w .2 .2 .2 .1) 
    (graphics-draw-line w .2 .1 .1 .1)) 

我看不出差異!

回答

6

不要只將表達式與() s分組 - 它會嘗試將第一個結果用作函數,但值爲#!unspecific - 絕對不是函數。

使用此:

(define (box d x1 y1 x2 y2) 
    (graphics-draw-line d x1 y1 x1 y2) 
    (graphics-draw-line d x1 y2 x2 y2) 
    (graphics-draw-line d x2 y2 x2 y1) 
    (graphics-draw-line d x2 y1 x1 y1)) 
+0

是的,就是這樣。在必要時我認爲perens。這是我的第一個計劃。 – 2011-06-07 22:25:03