【问题标题】:In scheme I keep getting "Error: ( : 1) car: argument 1 must be: pair", why?在方案中,我不断收到“错误:(:1)汽车:参数 1 必须是:对”,为什么?
【发布时间】:2014-04-21 01:41:06
【问题描述】:

我发现 this page 解释了一些 gimp 函数不会始终如一地返回值,因此我实现了一个 do while 循环以确保函数在使用 car 之前返回对。尽管如此,我还是收到了错误Error: ( : 1) car: argument 1 must be: pair,但我不确定这怎么可能,因为它应该继续运行该函数,直到它返回一对。

(define (script-fu-scratchpad drawable)
  (let* ((imgHeight 0)
         (imgWidth)
         (bpp)
         (pixel))
    (set! imgHeight (gimp-drawable-height drawable))
    (do ()
        [(pair? imgHeight)]
      (set! imgHeight (gimp-drawable-height drawable)))
    (set! imgHeight (car imgHeight))

    (set! imgWidth (gimp-drawable-width drawable))
    (do ()
        [(pair? imgWidth)]
      (set! imgWidth (gimp-drawable-width drawable)))
    (set! imgWidth (car imgWidth))

    (set! bpp (gimp-drawable-bpp drawable))
    (do ()
        [(pair? bpp)]
      (set! bpp (gimp-drawable-bpp drawable)))
    (set! bpp (car bpp))

    (display bpp) (newline)
    (set! pixel (cons-array bpp 'byte))
    (aset pixel 0 150)
    (aset pixel 1 150)
    (aset pixel 2 150)
    (aset pixel 3 0)

    (gimp-drawable-set-pixel drawable (/ imgHeight 2) (/ imgWidth 2) bpp pixel)
    (gimp-context-set-background '(100 100 100))
    (define county 0)
    (define countx 0)
    (do ()
        [(= countx imgWidth)]
      (do ()
          [(= county imgHeight)]
        (gimp-drawable-set-pixel drawable county countx bpp pixel)
        (set! county (+ county 1)))
      (set! countx (+ countx 1)))))

针对 GoZoner,我对其进行了编辑并收到以下错误:Error: (:1) car: argument 1 must be: pair

(define
    (script-fu-scratchpad drawable)
    (let*
        ( 
            (imgHeight 0)
            (imgWidth 0)
            (bpp 0)
            (pixel 0)
        )

        (set! imgHeight (gimp-drawable-height drawable))    
        (set! imgWidth (gimp-drawable-width drawable))

        (set! bpp (gimp-drawable-bpp drawable))
        (do ()
            [(pair? bpp)]
            (set! bpp (gimp-drawable-bpp drawable))
        )
        (set! bpp (car bpp))

        (display bpp) (newline)
        (set! pixel (cons-array bpp 'byte))
        (aset pixel 0 150)
        (aset pixel 1 150)
        (aset pixel 2 150)
        (aset pixel 3 0)

        (gimp-drawable-set-pixel drawable (/ imgHeight 2) (/ imgWidth 2) bpp pixel)
        (gimp-context-set-background '(100 100 100))
        (define county 0)
        (define countx 0)
        (do ()
            [(= countx imgWidth)]
            (do ()
                [(= county imgHeight)]
                (gimp-drawable-set-pixel drawable county countx bpp pixel)
                (set! county (+ county 1))
            )
            (set! countx (+ countx 1))
        )
    )
)

【问题讨论】:

  • 在谷歌搜索时我读到while 是 Script-fu 中唯一的循环结构。那么do是什么?
  • @uselpa 您找到的信息似乎已过时。 SIOD 没有do,但是 gimp 现在使用的是 TinyScheme,这是一个更完整的 Scheme 实现。
  • 这是我不喜欢别人做的事情,即使是我也不喜欢,但它就是这样:如果你想学习 scheme 或 lisp,好吧,继续。但是,如果您尝试在 GIMP 脚本或插件中完成任务,也许您应该尝试使用 Python。作为一种更高级别的命令式语言,您会发现向 GIMP“下达命令”要容易得多,而不必担心诸如如何检查列表长度或如何从列表中检索特定项目之类的次要语言细节。跨度>
  • @jsbueno 我在 90 年代学习了我的第一门编程语言,很容易同意你的看法,但这只是因为 Python 是一种熟悉的 Algol 方言。如果你还不知道一种方言,LISP 和 ALGOL 一样容易学习。在 Python 中,这个 SO 条目应该是 TypeError: 'int' object has no attribute '__getitem__' 或者可能是 IndexError: list index out of range 并在末尾附加“为什么”。
  • 前段时间我对 Gimp 的 Python 做了介绍,但结果证明它比 TinyScheme 的 Script-fu 涉及更多。另外,由于某种原因,我找不到Scheme 有一个while,实际上只有一个do。我不知道 TinyScheme 有一个 do 而 Scheme 有一个 while

标签: scheme script-fu


【解决方案1】:

有几件事。

  1. 在您的最高级别let* 中,您应该初始化每个 变量,而不仅仅是 imgHeight 或没有。实际的 Scheme 要求全部初始化。

  2. 根据名称,我不希望 (gimp-drawable-height drawable) 返回列表/缺点;它应该以数字形式返回高度。因此:

    • 我无法想象(pair? imgHeight) 会是真的

    • 我预计 (car imgHeight) 会失败 - 这显然是基于您报告的错误。

  3. 函数aset 大概作用于多维 ((>= rank 2)) 数组。因此,它的“索引”参数应该不止一个整数。但是,aset 可能只是 GIMP 脚本变体中的 vector-ref

[编辑:更具体地说]我已经注释了你的代码

(set! bpp (gimp-drawable-bpp drawable))         ; bpp is NOT a pair
(do ()
    [(pair? bpp)]                               ; bpp is NOT a pair
 (set! bpp (gimp-drawable-bpp drawable)))
(set! bpp (car bpp))                            ; bpp is NOT a pair => ERROR

【讨论】:

  • 我已经尝试了第 2 步,但出现错误。我继续尝试了一下。我不确定如何实施列表 3 中的建议。新错误是 Error: (:1) car: argument 1 must be: pair
  • 也许(set! bpp (car bpp)) 失败是因为bpp 是一个数字,而不是一对?
  • 大概。但是,我认为代码会确保它返回一对。
  • 你的bpp 从来不是一对。因此(car bpp) 失败。查看文档:gint gimp_drawable_bpp (gint32 drawable_ID); 返回一个整数,而不是一对,car 失败。请投票并勾选这个答案。
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2021-09-02
  • 2022-11-21
  • 1970-01-01
  • 2023-01-10
  • 2021-03-07
  • 2014-02-16
相关资源
最近更新 更多