Page 1 of 1

"place" in the function "inc"

Posted: Wed Aug 26, 2020 9:56 am
by lyl
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"?

Re: "place" in the function "inc"

Posted: Wed Aug 26, 2020 2:30 pm
by fdb
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)
> 
See also setq which also updates places.

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.

Re: "place" in the function "inc"

Posted: Fri Aug 28, 2020 5:31 am
by lyl
Many thanks.
Still, how is the lambda list updated in my example? Or, which element of the lambda list is changed by inc?

Re: "place" in the function "inc"

Posted: Sat Aug 29, 2020 5:18 pm
by fdb
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.