1
(define str '("3" "+" "3"))
(define list '(3 + 4))


(define (tokes str)
  (case (car str)
    ((or "+" "-" "*" "/")(write "operand")
                         (tokes (cdr str)))

                         (else (write "other"))
    ))

(define (tokelist)
  (case (car list)
    ((or "+" "-" "*" "/")(write "operand"))
    (else (write "other"))))
4

1 回答 1

3

当您使用列表时,您正在尝试将字符串"+"与过程进行比较。+这些是不同的类型,它们是不相等的。

试试这个:

> (string? "+")
#t
> (procedure? +)
#t
> (string? +)
#f

这应该让您对如何解决问题有一个很好的了解,但请注意:

> (= + +)
=: expects type <number> as 1st argument, given: #<procedure:+>;
other arguments were: #<procedure:+>

你需要:

> (equal? + +)
#t
> (equal? + "+")
#f
> (equal? "+" "+")
#t

使用这些想法,这应该可以让您的代码正常工作:

(define (plus? s)
    (if (procedure? s) (equal? + s) (equal? "+" s)))
于 2012-05-06T05:57:50.627 回答