In newLISP (almost) every expression returns a value when evaluated, so think of the two parentheses enclosing an expression as 'collapsing together' to produce a single value:
- the
if is a function and returns a value, either "yes" or "no" in this case:
Code: Select all
> (set 'x (if (= y 2) "yes" "no"))
"no"
> (set 'y 2)
2
> (set 'x (if (= y 2) "yes" "no"))
"yes"
A user-defined function also returns a single value when evaluated:
Code: Select all
(define (action1)
"this is action 1")
(define (my-func)
(local (x)
(if (= blah 2)
(action1))))
(set 'blah 2)
(my-func)
;-> "this is action 1"
In this definition of
my-func, the value returned by the function is the value of the last evaluation. Here, it's the string returned by the last evaluation in the function
action1.
Getting closer to your Perl: you might find it helpful to use a more Lisp-y construct, such as
cond.
Code: Select all
(define (my-func a)
(cond
((= a "blah") 1)
((= a "blip") -1)
(true 0)))
(my-func "blah")
;-> 1
(my-func "blab")
;-> 0
(my-func)
;-> 0
Notice how the '1' is returned by the
cond, then returned in turn by the
my-func. The "true" clause is the "if nothing else" bit; it's the last evaluation of the function, so the function returns 0 if nothing else matches.
The advantage of
cond is that you can easily do two or more actions for each 'case':
Code: Select all
(define (my-func a)
(cond
((= a "blah") (println "'we have a blah'")
(set 'self-destruct 'yes))
((= a "blip") (println "'we have a blip'")
(set 'self-destruct 'cancel))
(true (println "'we got nothing'")
0)))
> (my-func "blah")
'we have a blah'
yes
> (my-func "blip")
'we have a blip'
cancel
> (my-func "")
'we got nothing'
0
>
Sometimes you can't organize your code to make this work. So you have to remember to return the symbol as the last action of the function:
Code: Select all
(define (my-func a)
(let ((self-destruct 'cancel))
(cond
((= a "blah") (set 'self-destruct 'yes))
((= a "blip") (set 'self-destruct 'cancel))
(true (println "it's all gone wrong")))
self-destruct))
What's harder to do in newLISP is your idea of explicitly returning from different places in a function. Use the built-in branching provided by if, cond, etc...
I'm writing too much - stop me!