在方案中打印

Printing in Scheme

我在使用条件后在方案中打印字符串时遇到一些困难,并收到以下错误:

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #t
  arguments...:

我似乎无法弄清楚它有什么问题,希望得到任何帮助。

(define (neg int)
  (cond
    (((< int 0) (display "negative"))
     (* int (-1)))))

您的条件开头有太多括号;你需要两个,而不是三个。请记住,在 Scheme 中,如果您将 () 之间的内容包围起来,就会成为函数应用程序!这就是为什么这没有意义:(-1),因为 -1 不是一个函数,它是一个数字。另外,如果这个值不是负数,你会怎么做?你也需要处理那个案子!试试这个:

(define (neg int)
  (cond ((< int 0)
         (display "negative ")
         (* int -1)) ; or better: (- int)
        (else
         (display "positive ")
         int)))