简体   繁体   English

scheme-字符串附加错误的类型以应用错误

[英]scheme - string-appends wrong type to apply error

I have this code 我有这个代码

(lambda (symbol)
  (let*(
        (datalist (get-list symbol))
    (desc " ")
        (html "<table border=\"1\">")
        (html (string-append html "<tr><td>" (list-ref datalist 1) "</td><t\
r><td>" (list-ref datalist  2) "</td></tr>"))
        )
    (do ((p 7 (+ 7 p)))
    ((> p (-(length datalist) 2)))
      (desc (string-append desc "<tr><td>"(list-ref datalist p) "</td><td>"\
 (list-ref datalist (+ p 1))"</td></tr>"))
      )
    (set! html (string-append html desc "</table>"))
      html
    )
  )

I'm basically taking some elements from a datalist and adding some html tags to them. 我基本上是从数据列表中获取一些元素,并向其中添加一些html标签。 However, when I run the code, I get a "wrong type to apply error" on the desc binding (line 4). 但是,当我运行代码时,我在desc绑定(第4行)上收到“错误类型以应用错误”。 What gives? 是什么赋予了? Even when i change it to values such as "foo", i still get the wrong type error. 即使我将其更改为“ foo”之类的值,我仍然会收到错误的类型错误。

Any suggestions? 有什么建议么?

The error is not in the binding, is in the loop's body: 错误不在绑定中,而是在循环体内:

(desc (string-append desc …))

You're trying to apply desc as if it were a procedure - but it's a string. 您正在尝试将desc当作过程来应用,但这是一个字符串。 So basically that part of the code is doing something equivalent to this: 因此,基本上,部分代码正在执行与此等效的操作:

("s1" "s2")

Which will result in the reported error. 这将导致报告的错误。 Perhaps you meant to do this? 也许您打算这样做?

(set! desc (string-append desc …))

I'll take a guess to figure out what were you trying to implement. 我会猜测一下您要实现的目标。 And do notice how properly indenting and formatting the code helps to make it clear: 注意缩进和格式化代码的正确性如何有助于使代码更清晰:

(lambda (symbol)
  (let* ((desc " ")
         (datalist (get-list symbol))
         (html (string-append "<table border=\"1\">"
                              "<tr><td>"
                              (list-ref datalist 1)
                              "</td><tr><td>"
                              (list-ref datalist  2)
                              "</td></tr>")))
    (do ((p 7 (+ 7 p)))
      ((> p (- (length datalist) 2)))
      (set! desc (string-append desc 
                                "<tr><td>"
                                (list-ref datalist p) 
                                "</td><td>"
                                (list-ref datalist (+ p 1))
                                "</td></tr>")))
    (string-append html desc "</table>")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM