You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DrRacket实现SICP图像语言George.img时遇参数不匹配错误

Fixing the Arity Mismatch Error with 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:

  1. 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 window
    
  2. Define your custom draw-line that 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)))
    
  3. Now your existing make-picture code should work without the arity error! This custom draw-line matches 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:57:30