你能解释一下为什么这适用于字符串而不是列表吗?

时间:2012-05-06 05:46:18

标签: scheme racket

(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"))))

1 个答案:

答案 0 :(得分: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)))