Hi octowuss, and welcome!
octowuss wrote: ↑Fri Jan 01, 2021 7:30 am
That doesn't make any sense [...] and wanted to learn more Lisp, but this version is full of these incomprehensible "gimmicks" that make it very difficult to work out how to use it!
Well, the OP was really asking about a very obscure "corner case" usage of `inc` to accomplish the task of `sum` and it's definitely not a normal usage (which is why you couldn't find it in the manual).
When, I first saw this many years ago, I had already been programming for many years in newLISP and didn't even know about it. OTOH, it didn't affect my programming use cases at all (and probably nobody else's), because there is an idiomatic way to accomplish the same thing.
If you want to learn about how to define and use functions that "have memory" (like `sum`), consult the manual here:
http://www.newlisp.org/downloads/newlis ... unc_memory. The example right there actually has a version of `sum`! (It's just has another name.) Rewriting that here for our convenience, we have:
Code: Select all
> (define (sum:sum x) (inc sum:current-total x))
(lambda (x) (inc sum:current-total x))
> (sum 1)
1
> (sum 1)
2
> (sum 2)
4
> (sum 3)
7
>
And the function definition has not been altered (per your requirement):
Code: Select all
> sum:sum
(lambda (x) (inc sum:current-total x))
>
octowuss wrote: ↑Fri Jan 01, 2021 7:30 am
Also, why does the example have a 0 after the x in the parameter list for sum?
I can leave that out and the function still works!
That's just newLISP's way of setting a default value for a function parameter, to be used if the caller doesn't supply an argument value for it. Many languages have this feature. newLISP documents this fact in its manual here:
http://www.newlisp.org/downloads/newlis ... tml#define
I hope you stick around and continue to use newLISP. It is a very fun language! Happy hacking!
(λx. x x) (λx. x x)