Search code examples
recursionnewlisp

newLISP Invalid Function


I have a homework assignment where we need to write some functions in newLISP. I am running into an issue, so I made an example of the issue to see if anyone can help me out.

The issue is that after a recursive function is over, it returns an ERR: invalid function : error. This happens no matter what the function I am calling is.

For an example, I made a recursive function that decrements a number until we hit 0. This is the code:

 (define (decrement num)
   (if (> num 0)
     (
       (println num)
       (decrement (- num 1))
     ) 
     
     (
       (println "done")
     )
   ) 
 )

Whenever I run this function, starting with the number 10, the output looks like this:

> (decrement 10)
10
9
8
7
6
5
4
3
2
1
done
ERR: invalid function : ((println "done"))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement 10)

I cannot figure out why this is returning an invalid function error. I know very little about newLISP, so this may be a simple question.

Thanks!


Solution

  • In Lisp you don't use arbitrary parentheses to group things together. So you should do something like this:

    (define (decrement num)
       (if (> num 0)
          (begin
            (println num)
            (decrement (- num 1))
          )
          (println "done")
       ) 
     )