Search code examples
syntaxlispcommon-lisp

file-stream character ends with an object


I am new in Lisp, just did some tutorials before. This time I try to create my own code. I want to print all odd numbers from 1 to 20.

I tried to shift the brackets and syntax things like that. This is my code:

(loop for x from 1 to 20

   (cond

   ((/= 0 (mod x 2))

   )

   do (print x)

)

And as a result I get:

*** - READ: input stream
      #<INPUT BUFFERED FILE-STREAM CHARACTER #P"main.lisp" @7> ends within an
      object. Last opening parenthesis probably in line 1.

Is there anybody who can help me?


Solution

  • Is there anybody who can help me?

    sure, you just need to balance your code or count the parentheses to see the error:

    (loop for x from 1 to 20
    
       (cond
    
       ((/= 0 (mod x 2))
    
       )
    
       do (print x)
    
    )
    

    Above has 6 opening and 5 closing parentheses...

    You need to check the syntax:

    • unbalanced parenthesis
    • illegal LOOP syntax

    Here is a booklet with an overview of Common Lisp syntax: Common Lisp Quick Reference

    Also: manually indenting Lisp code will not work: use the editor to do that.

    CL-USER 6 > (loop for x from 1 to 20
                      do (when (/= 0 (mod x 2))
                           (print x)))
    
    1 
    3 
    5 
    7 
    9 
    11 
    13 
    15 
    17 
    19 
    NIL