需要帮助进行方案编程

时间:2016-10-18 16:30:15

标签: list lambda functional-programming scheme lisp

我希望程序计算在表达式中找到特定子字符串的次数。我编写了以下代码,但我没有得到预期的输出。

(define (count-occurrences s slist)
    (if (null? slist)
            0
        (+ (count-occurrences-in-s-sexp s (car slist))
           (count-occurrences s (cdr slist)))))

(define (count-occurrences-in-s-sexp s sexp)
    (if (symbol? sexp)
            (if (eqv? sexp s) 1 0)
        (count-occurrences s sexp)))

Input: (count-occur '(a x) '((x y z) x (z (a x) y)) )
Output: 0
Expected O/p : 1

Input: (count-occur 'x '((x y z) x (z (a x) y)))
Output: 3
Expected O/p : 3

当我提供list的输入时。我没有得到预期的输出。任何人都可以帮助我:/

1 个答案:

答案 0 :(得分:0)

您希望使用equal?代替eqv?来确定两个列表是否相同。

相关问题