-1
(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. 
An Overflowed Stack
  • 334
  • 1
  • 5
  • 20

1 Answers1

1

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.

Community
  • 1
  • 1
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Somehow it is all a matter of placing the wrong parens in the wrong places. I'm a CL newbie too, but not new at programming. To me, the editor turned out to be a good help in placing them at the right places. – Rudy Velthuis Nov 20 '14 at 22:15