Questions on Syntax from a newLisp Newbie

Started by Sunburned Surveyor, January 17, 2005, 07:56:50 AM

Previous topic - Next topic

Sunburned Surveyor

I had a couple of questions about the newLisp syntax I was hoping you guys could help with.



(1) I believe a symbol is like a variable in other languages. It is a named container that holds a value. Is this correct?



(2) What is the difference between a symbol and a constant?



(3) On page 13 pf the reference manual it discusses resetting the value of the integer math operators. It has the following LISP statement:



(constant '+ add)



I realize this statement is setting the value of the "+" operator to the add function, but what is the purpose of the single quote? Are we using that to refer to the name "+" rather than evauluating it as a math operator?



(4) Is init.lsp used to load a set of user defined functions for the newLisp compiler/IDE?



(5) I did some on-line research about Lambda ecpressions. I want to make sure I understand them correctly. Is the following true:



A Lambda expression defines a function temporarily. The first part of the expression must be the symbol name Lambda, the second is a parameter/argument list, and the thirs is the expression that operates on the arguments.





Thanks for any info.



The Sunburned Surveyor

HPW

#1
I give it a try:



>(1) I believe a symbol is like a variable in other languages. It is a named container that holds a value. Is this correct?



Not only a value. It can also contain a function or list. There is no sharp frontier between code and data in lisp. It is one of the powerfull features and can be used for code generation at runtime.



>(2) What is the difference between a symbol and a constant?



The constant is protected against overwriting and the symbol not.



>I realize this statement is setting the value of the "+" operator to the add function, but what is the purpose of the single quote? Are we using that to refer to the name "+" rather than evauluating it as a math operator?



The quote prevent the symbol against evaluation to its content. You can also write (constant (quote +) add). It is the same.



>(4) Is init.lsp used to load a set of user defined functions for the newLisp compiler/IDE?



It is used to load your own function into the newlisp enviroment. It is not primaly used for the IDE.

Therefor is the newlisp-tk.config where you can use TCL-syntax to initialise TK.
Hans-Peter

Ryon

#2
Thanks for asking these questions, sunburn. I'm still lost at (+ 2 2).
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"

HPW

#3
>I'm still lost at (+ 2 2)



Joking or serious? ;-)



Ok, the UPN notation is not for everyone taste, but it has advantages at the second look together with the functional programming style in LISP.
Hans-Peter

Ryon

#4
>> I'm still lost at (+ 2 2)

> Joking or serious? ;-)



Joking, about my serious inability to think in the language.



Yes, I recognize the beauty of NewLISP, and I am in awe of Lutz' and others elegant solutions to common computer tasks. But I could stare at a one-page listing for a month and not "get" how this is accomplished.



My failing.



Much of my problem is in visualizing what the machine is doing. I can look at a helicopter rotor hub and tell you what each and every part does. I can feel every complex motion of the assembly as it spins up, lifts off, moves to translational flight, then back to hover. But I just don't feel the lisp machine.



I can glance at a listing in the Forth computer language and immediately see where the parts are. With a bit of study, I know that a value is placed on the stack, to be used by a "word" which returns its results back to the stack. And I can count on the "word" to reliably remove, transform, and replace stack items each time the mechanism "rotates". From that, I can see in my mind's eye how the Forth program works. I just don't have this same intuition when using NewLISP.



Both NewLISP and Forth are extremely compact, elegant and beautiful ways of stating a set of instructions. But, for me the NewLISP machine is still a mystery. I can see the assembled parts, but have no feel of how they move. Maybe it's because the lisp virtual machine is not graphically explained. Or maybe it's because its mechanisms are explained in terms that I have no feeling for: Dynamic vs. lexical scoping. Parameter passing by value. Tell me what this means: "This feature can be used to create functions, which look like normal functions, but really are context objects with a method whose name is identical to the context name and can update their lexically isolated static variables." Ouch! Maybe a picture or two would be helpful?



I am sure that most of you do visualize the NewLISP machine as it works. It is my own failing that I am not able to do so.  I think that Lutz has done a tremendous service in giving us this language, in   providing simple, clear examples, and then patiently explaining what he has shown to us already. As I said, I stand in awe of the man. And I truly enjoy the discussion that surrounds his work here in this forum. Even if I can't follow 90% percent of what is said.



So thank you Sunburned Surveyor, for asking the questions that I can't even formulate. Maybe if I read enough of the great postings here, I'll catch on, eventually.
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"

Lutz

#5
Perhaps what is missing is a basic explanation of how evaluation works in newLISP. Look into John Small's tutorial at: http://www.atlaol.net/newlisp/tutorial.html">http://www.atlaol.net/newlisp/tutorial.html



he does an excellent job explaining things.



Lutz

eddier

#6
Nice tutorial.



Coming from the imperative side of things, I also faced such challenges. The biggest one for me was (f x0 x1 ... xn) in Lisp == f(x0, x1, ..., xn) in Python, Perl, C, Java, etc,... Once I figured out that the first thing after the beginning "(" was an operation that operated on everything up to the ")" I started to get into it a bit. Then I realized to build programs in the functional world, just compose the functions. In the mathematical and imperative world (g comp f)(x) = g(f(x)), but in lisp it's just (g (f x)) => first apply f to x then apply g to whatever f left. This makes a pretty picture. Lisp is just evaluating a tree, the same infix, postfix, prefix trees you learned in that second semester programming course. As example,



(+ 2 3 (* 4 3))

  [+]
 / |
2  3  [*]
      /  
    4     3


Or, you can look at it as a bunch of linked lists. Each node has two pieces, in Lutz's model, the left part of the first node is the operation, the right part points to its argument list. The values are in the left part of each node. If the left part of a node is a pointer, then follow that pointer down. This will be a new list to evaluate. The down pointer will always be a function to evaluate unless it is a quote function.

root
 |
 V
[+ : -]-->[2 : -]-->[3 : -]-->[  : nil]
                               |
                               V
                               [* : -]-->[4 : -]-->[3 : -]-->[  : nil]


Lisp will try to evaluate every list even if it doesn't make sense. Sometimes (a bunch of times) you will want to treat a list as data. To do keep lisp from evaluating it, use the quote function or use the short hand of putting a ' in front of the list. Then you can pass a list from one function to the next.

Example

(join '("hello" "world") ":") => "hello world"

Here join is the function and it's arguments are '("hello" "world") and ":". The reason '("hello" "world") is data and lisp is not trying to evaluate "here" as a function is because of the ' in front.



I hope this helps.



Eddie

eddier

#7
Sorry, I couldn't get the down links to  line up correctly, in the linked list code above the down arrow should be below the [ : nil] not the middle part of [3 : ].

Eddie

Ryon

#8
How could I have missed http://www.atlaol.net/newlisp/tutorial.html">//http://www.atlaol.net/newlisp/tutorial.html? Yes, this is exactly the sort of information that I need, though I doubt that I'll be able to do it all in 21 minutes!



And I did notice that there was a problem with:



(join '("hello" "world") ":") => "hello world"



though it surprised me where the missing ":" showed up in the answer. Plenty here to study. Thanks!
\"Give me a Kaypro 64 and a dial tone, and I can do anything!\"

Lutz

#9
You can draw it even simpler:



(+ 2 3 (* 4 3))

[ ]
 
  [+] -> [2] -> [3] -> [ ]
                         
                         [*] -> [4] -> [3]


because newLISP has no dotted pairs, a lisp cell has just a contents and the pointer to the next cell.



Lutz

Lutz

#10
Ryon, let us know if that tutorial did it for you and let jsmall know where it could be improved.



I would also be great to hear from others, what the biggest barriers are when coming to newLISP with or without previous LISP experience.



Lutz

eddier

#11
Oops. Yes I made a mistake.



(join ("hello" "world") ":") => "hello:world"



Also, Lutz, I think there should be a "->[ ]" after the "[*]->[4]->[3]" correct?



After looking at a bunch of Lisp dialects, schemes, goo, and NEWLisp, I'm finding things I like  about all of the different dialects and language design. I took language design along time ago, but we mostly studied algol, pascal, and ada. Very little about Lisp, prolog, and smalltalk, and nothing about Forth. But then, a lot has changed since the mid 80s.



Eddie

Lutz

#12
In the diagram the [ ] stands for some kind of 'list envelope' for it's members: *, 2, 3 and this envelope itself is a lisp cell the content of which is a pointer to the first list member, in this case the *.



Lutz