使用DrRacket实现SICP图像语言George.img时遇参数不匹配错误
draw-line in SICP's Picture Language Hey there! Let's break down the problem you're hitting while building George.img in DrRacket for SICP Chapter 2.
The Root Cause
That arity mismatch error spells out exactly what's going wrong: the draw-line procedure your code is calling expects 1 argument, but your code is passing it 2 transformed points (the start and end of your segment).
The core issue is that DrRacket's built-in or library-provided draw-line (in your case, from sn-less-unit.rkt) doesn't match the interface described in SICP. The book defines draw-line as a procedure that takes two coordinate pairs (points) and draws a line between them—but the version you're using right now is built for a different input format.
The Short Answer
Yes, you absolutely need to define your own draw-line procedure that aligns with the SICP specification. DrRacket doesn't ship with an exact implementation of the book's picture language abstractions, so you'll have to bridge that gap yourself.
How to Implement the Correct draw-line
Here's a step-by-step solution using DrRacket's racket/draw library to create a canvas and render your picture:
First, set up a basic drawing window and canvas:
(require racket/draw) ; Create a window to display your picture (define main-frame (new frame% [label "SICP George.img"] [width 500] [height 500])) (define drawing-canvas (new canvas% [parent main-frame])) (define drawing-dc (send drawing-canvas get-dc)) ; Get the drawing context (send main-frame show #t) ; Show the windowDefine your custom
draw-linethat accepts two(x . y)point pairs and uses the drawing context to render the line:(define (draw-line start-point end-point) ; Extract x/y coordinates from each point pair for DrRacket's native API (send drawing-dc draw-line (car start-point) (cdr start-point) (car end-point) (cdr end-point)))Now your existing
make-picturecode should work without the arity error! This customdraw-linematches the SICP interface—it takes two points, converts their pair values into the raw coordinates DrRacket's drawing API expects, and renders the line between them.
Additional Notes
- Make sure all other SICP picture language abstractions are correctly implemented too:
frame-coord-map,start-segment,end-segment, and any segment/list constructors you're using. These are all part of the book's custom abstraction layer, so defining them yourself will help reinforce the concepts. - If you're using a specific SICP starter library for DrRacket, double-check if it includes a compatible
draw-line—you might just need to adjust your imports instead of writing it from scratch.
内容的提问来源于stack exchange,提问作者hoho5702

