Why does my lisp code give me …should be a lambda expression?

前端 未结 2 1086
谎友^
谎友^ 2020-12-12 05:24
(defun helper-2 (list) 
  (if (null (first (rest list)))
      0)
  (+ (distance ((car list) (first (rest list)))) 
     (helper-2 (rest list))))

I

相关标签:
2条回答
  • 2020-12-12 06:01

    Your helper-2 function is wrong in two places:

    1. You should be using a two-armed if, so that it functions as an if/else.
    2. You have too many parentheses around the (car list).

    Here's the fixed version:

    (defun helper-2 (list) 
      (if (null (first (rest list)))
          0
          (+ (distance (car list) (first (rest list))) 
             (helper-2 (rest list)))))
    
    0 讨论(0)
  • 2020-12-12 06:07

    Section 3.1.2.1.2.3 Function Forms of the HyperSpec describes how a form that is a cons, such as ((car list) (first (rest list))), is evaluated:

    How a compound form is processed depends on whether it is classified as a special form, a macro form, a function form, or a lambda form.

    You can read the subsections linked to from that page for more details, but the essence is that since the first element of this list is not a symbol, the form as a whole must be a lambda form. According to 3.1.2.1.2.4 Lambda Forms a lambda form is a list where the first element is a lambda expression. `However, (car list) is not a lambda expression, so you get the corresponding error message.

    You claimed that (distance '(2 0) '(4 0)) will output two, but that shows distance being called with two arguments. Even if ((car list) (first (rest list))) could be evaluated, it would produce just one value, and so (distance ((car list) (first (rest list)))) would call distance with just one argument. You should be doing this instead:

    (distance (car list) (first (rest list)))
    

    Also see:

    • Cannot use function call as first argument in s-exp
    • Why do we need funcall in lisp
    • Do any lisps have a s-expression as their head, e.g. ((f 2) 3 4)? If not, why?
    0 讨论(0)
提交回复
热议问题