-1

I have a function,format-ls:

(defun format-ls (ls)
    (let (( acc ()))
      (dolist (elt ls)
         (push "(~A . " acc))
    acc))

You can notice that there is a missing parenthesis in the closing line, acc)).

Yet my Lisp REPL(sbcl 2.1.1) interprets this expression without any errors:

format-ls, OK

But if I add the missing parenthesis, as shown below:

(defun format-ls (ls)
    (let (( acc ()))
      (dolist (elt ls)
         (push "(~A . " acc)))
    acc))

The REPL throws out the following error:

format-ls,error

Now this expression where all the parenthesis are matched, will be interpreted without any issues:

(b)
(defun post+ (ls)
    (let (( acc ()))
       (let ((i -1))
        (dolist (elt ls)
         (push (+ elt (setf i(+ i 1))) acc)))
    (reverse acc)))

post+

What am I missing here?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Hxx
  • 1
  • What you're missing is that there's no mismatched paren in the first example, and there is (an extra closing paren) in the second. Are you treating the paren inside the string as one of the parens to match? –  Mar 25 '21 at 15:00
  • @Rainer Joswig - Thank you for taking on your time to go through my temporary(I hope...) mental lapse. I should have known it was time for a coffee break. – Hxx Mar 25 '21 at 20:52
  • 1
    Your indentation has a problem. `acc` is in the scope of the `let`, so references to it must be indented more than the `(let...`. It could be that your editor is doing this due to being confused by a parenthesis inside a string literal. – Kaz Mar 26 '21 at 18:22

1 Answers1

1
(defun format-ls (ls)
  (let ((acc ()))
    (dolist (elt ls)
      (push "(~A . " acc))
    acc))

There are no unmatched parentheses in the s-expression. There is a single open parentheses inside a string, though.

It reads just fine:

CL-USER 40 > (read-from-string "(defun format-ls (ls)
                                 (let ((acc ()))
                                   (dolist (elt ls)
                                     (push \"(~A . \" acc))
                                   acc))")
(DEFUN FORMAT-LS (LS)
  (LET ((ACC NIL))
    (DOLIST (ELT LS) (PUSH "(~A . " ACC)) ACC))
221
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346