How do I pass in a list of list into a function?

2019-09-21 00:58发布

(defun square (n) (* n n))

(defun distance (a b)
    (let ( 
        (h (- (second b) (second a)))
        (w (- (first b) (first a))))

        (sqrt (+ (square h) (square w)))
        )
)
(defun helper-2 (head) 
    (if (null (first (rest head))))
        0
    (+ 
        (distance (car head) (first (rest head))) 
            (helper-2 (rest head))
    )

I have this code written. My question is how do I use the helper-2 method? I've tried

(helper-2 '((2 0) (4 0)))
(helper-2 '(2 0) '(4 0)) neither works. Could anyone help? Thanks. 

1条回答
该账号已被封号
2楼-- · 2019-09-21 01:40

Please cut and paste the code in my previous answer and use it verbatim. Here, you've actually made some changes to the code to make it incorrect: instead of your original one-armed if, you've made it even worse, a zero-armed if.

A correctly-formatted two-armed if expression looks like this (noting that expr1 and expr2 are supposed to be flush with (indented to the same level as) test):

(if test
    expr1
    expr2)

This means that if test evaluates to a truthy value (anything other than nil), then expr1 is evaluated, and its value is the value of the if expression; otherwise expr2 is used instead. In the code snippet I had, test is (null (first (rest list))), expr1 is 0, and expr2 is (+ (distance (car list) (first (rest list))) (helper-2 (rest list))).


The other nice thing about using my code snippet directly is that it's already formatted correctly for standard Lisp style, which makes it much more pleasant for other Lisp programmers to read.

查看更多
登录 后发表回答