Scheme getting error is not a function

2019-06-05 00:26发布

问题:

,i am doing my assignment in Scheme. I am using Scheme MIT Interpreter and https://repl.it/languages/scheme to test my code.

First question is

; - in? procedure takes an element ‘el’ and a list ‘lst’. ; - It returns a boolean value. ; - When el is in lst it returns true, otherwise returns false. ; - Examples: ; (in? 3 ’(2 5 3)) ; evaluates to #t ; (in? 2 ’(1 (2) 5)) ; evaluates to #f ; - If lst is not a list, it produces an error..

my code is

(define lst())
(define el())

(define in? (lambda (el lst)
    (if(null? lst) 
        #f
 (if (eq? (car lst el ))
    #t
   (in? el cdr lst )))
(error "ERROR")))

(in? 3'(2 5 3))

I got error in MIT Interpreter below

The procedure #[compiled-procedure 13 ("global" #x14) #x14 #x2620cd4] has been called with 3 arguments; it requires exactly 2 arguments. ;To continue, call RESTART with an option number: ; (RESTART 1) => Return to read-eval-print level 1.

and when i test it in https://repl.it/languages/scheme

i got error like

Error: 2 is not a function [(anon)]

Why i am getting these errors?

回答1:

Try this:

(define in? 
  (lambda (el lst)
    (if (or (null? lst) (pair? lst))
        (if (null? lst) 
            #f
            (if (equal? (car lst) el )
                #t
                (in? el (cdr lst))))
        (error "ERROR"))))

The usual tips apply: be careful with the parentheses, indent correctly your code, use equal? for equality comparisons, notice the correct way to test if the parameter is a list and make sure you understand how to pass parameters to a procedure and how to actually call a procedure. It works as expected now:

(in? 1 '(2 5 3))
=> #f
(in? 3 '(2 5 3))
=> #t
(in? 1 5)
=> ERROR


标签: scheme