Scheme “Error: (4 6 5 87 7) is not a function”

2019-09-09 22:56发布

问题:

I just asked a similar question and got the answer I needed, but his time I cannot find any extra parenthesis that would be causing this error: "Error: (4 6 5 87 7) is not a function". Is it due to something else? My code takes a number and if it is already in the list, it does not add it. If the list does not already contain the number, then it adds it.

(define (insert x ls)  
        (insertHelper x ls '() 0)  
)

(define (insertHelper x ls lsReturn counter)
        (cond
            (
                (and (null? ls) (= counter 0)) 
                (reverse (cons x lsReturn))
            )
            (
                (and (null? ls) (>= counter 1))
                (reverse lsReturn)
            )
            (
                (eqv? x (car ls))
                (insertHelper x (cdr ls) (cons (car ls) lsReturn) (+ counter 1))
            )
            (
                else ((insertHelper x (cdr ls) (cons (car ls) lsReturn) (+ counter 0)))
            )

        )
)



(define theSet (list 4 6 5 87))
(display theSet)
(display "\n")
(display (insert 7 theSet))

回答1:

You suffer from a case of excessive parentheses, sprinkled with bad indentation. This should cure your ailments:

(define (insert x ls)  
  (insertHelper x ls '() 0))

(define (insertHelper x ls lsReturn counter)
  (cond ((and (null? ls) (= counter 0))
         (reverse (cons x lsReturn)))
        ((and (null? ls) (>= counter 1))
         (reverse lsReturn))
        ((eqv? x (car ls))
         (insertHelper x (cdr ls) (cons (car ls) lsReturn) (+ counter 1)))
        (else
         (insertHelper x (cdr ls) (cons (car ls) lsReturn) (+ counter 0)))))


回答2:

You have too many parentheses after the else.

For what it's worth, almost all instances of "foo is not a procedure" errors I see on Stack Overflow are caused by extraneous parentheses.



标签: scheme