## "place" in the function "inc"

Pondering the philosophy behind the language
lyl
Posts: 44
Joined: Sun Mar 25, 2018 5:00 am

### "place" in the function "inc"

The meaning of "place" in the syntax: (inc place [num]) is:
either a symbol or a place in a list structure holding a number, or a number returned by an expression.(from the newlisp manuel)

I still don't quite understand the real meaning of "place", as in the following code coming from 《Code Patterns in newLISP》:

Code: Select all

``````;; sum accumulator
(define (sum (x 0)) (inc 0 x))

(sum 1)    → 1
(sum 2)    → 3
(sum 100)  → 103
(sum)      → 103

sum  → (lambda ((x 0)) (inc 103 x))``````
In this example, what is the "place, and why"?

fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

### Re: "place" in the function "inc"

It refers to a place in a list, so you can do:

Code: Select all

``````> (set 'places '(0 0 0))
(0 0 0)
> (inc (places 1))
1
> places
(0 1 0)
>
``````

The example from Code Patterns illustrates that a function is also a list (a lambda list) and you can also update that list with inc.

lyl
Posts: 44
Joined: Sun Mar 25, 2018 5:00 am

### Re: "place" in the function "inc"

Many thanks.
Still, how is the lambda list updated in my example? Or, which element of the lambda list is changed by inc?

fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

### Re: "place" in the function "inc"

In your example the 0 after inc in the lambda list is changed by inc.

Your lambda list says :(define (sum (x 0)) (inc 0 x))

after you do (sum 1) your lambda list is :
(define (sum (x 0)) (inc 1 x)), so the 0 has become a 1.

after you do another (sum 1) your lambda list is :
(define (sum (x 0)) (inc 2 x)), so the 1 has become a 2.

octowuss
Posts: 1
Joined: Fri Jan 01, 2021 7:14 am

### Re: "place" in the function "inc"

That doesn't make any sense - how does the inc place refer to the location in the function sum to update?
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!

Code: Select all

``````> (define (sum (x)) (inc 0 x))
(lambda ((x)) (inc 0 x))
> (sum 1)
1
> (sum 5)
6
> sum
(lambda ((x)) (inc 6 x))``````
The documentation for inc says that "place is either a symbol or a place in a list structure holding a number, or a number returned by an expression."
Well, 0 looks like a "number returned by an expression", so I would expect that (inc 0 x) should just return the value of incrementing 0 by x - i.e. just the value x!
So, it's just not clear how the sum body gets updated! I want a function to be immutable - I don't expect an expression in the body to magically modify the function! This language looks interesting - I come from a TCL background 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! The documentation is woefully inadequate. Sorry.

rickyboy
Posts: 604
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

### Re: "place" in the function "inc"

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)