Search code examples
loopssyntax-errorlispelisp

elisp: syntax error on do clause


I am writing a Greatest Common Factor function.

I kept getting errors saying

"(if t (set q).....)" is not valid syntax

and so I commented it out. But then I was told that my do syntax is invalid.

syntax error on the 'do' clause

But I'm looking at it and seeing no errors. Why will my code not work?

(defun myGCD (a b)
"My function, which returns the Greatest Common Factor"
(let ((x a) (y b) (z 0)) 
  (loop until (or (zerop x) (zerop y))
    do ( ;(progn
      ;(if t ;(< a b)
        ; If case
        ;((setq b (- b a)) (setq c a))
        ; Else case
        ;((setq a (- a b)) (setq c b))
      ;)
      ;(return c)
    );)
  )
)
)

(myGCD 10 20)

Solution

  • You have basic syntax errors in your code.

    In a LOOP form you need to have one or more compound forms after a DO.

    (loop ... do () )
    

    is not allowed, because () is not a compound form.

    Also note that parentheses are not grouping expressions in a sequence.

    ((foo 1) (bar 2)) 
    

    above is not valid Lisp. In Scheme it might be correct, but there the first form then needs to return a function.

    To group expressions you would need something like progn, which allows embedded forms and returns the values of the last one:

    (progn
      (foo 4)
      (bar 2))
    

    Above is valid.