"place" in the function "inc"

Started by lyl, August 26, 2020, 02:56:32 AM

Previous topic - Next topic

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》:
;; 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

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

> (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.

lyl

#2
Many thanks.

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

fdb

#3
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

#4
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!


> (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

#5
Hi octowuss, and welcome!


Quote from: octowuss post_id=24979 time=1609486240 user_id=1457
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/newlisp_manual.html#func_memory">http://www.newlisp.org/downloads/newlis ... unc_memory">http://www.newlisp.org/downloads/newlisp_manual.html#func_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:


> (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):


> sum:sum
(lambda (x) (inc sum:current-total x))
>


Quote from: octowuss post_id=24979 time=1609486240 user_id=1457
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/newlisp_manual.html#define">http://www.newlisp.org/downloads/newlis ... tml#define">http://www.newlisp.org/downloads/newlisp_manual.html#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)