This page is under construction… RFC
newLISP logo by Brian Grayless.
This is my newLISP page; here you find some code sampels and other things connected to newLISP or LISP in general.
There exist many dialects and implementations of LISP, some used in special applications like elisp in Emacs or AutoLisp in AutoCAD. Common Lisp and Scheme are some of the fameous dialects. You can find a free implementation of Scheme at MIT/GNU Scheme.
Here is a page with a list of LISP implementations ALU Wiki: Implementation
On this page I write about the newLISP dialect, but much of the code can be used with other dialects, sometimes with just a little modification.
Get started
Get newLISP
To start testing newLisp, first go to http://newlisp.org/ and look in the download section. There you find information for how to set it up for your system. That page is also the most important page for anyone who wants to learn about newLISP.
Working flow
When starting the newLisp interpreter you see some information text and a command prompt. Just enter something you want to be interpreted. This is a very good thing, makes it easy to test things and is very funny.
You can test to write this, not the “>”, and you will get 11 as a result. In newLisp we write in prefix, also called Polish, notation. So the “+”, i.e. the operator or function name, is before not between the numbers i.e. the operands or function parameter.
>(+ 7 4)
If you are writing scripts you can test small parts of it in the debug process. The edit-interpret-debug cycle is often faster than the edit-compile-link-run-debug cycle when using a compiler.
When you write longer scripts you don’t want to write them all over again all the time. Instead you save it in a text file and load the textfile, it will then be interpret. This means that you can use almost any texteditor you like.
Using newLisp-tk you can use the load source bottom, otherwise you can write:
>(load “textfile.lsp”)
A tips is to define a function in the text file, then you can use it after the interpret. You can also use this method to separate your scripts in different parts.
Just to make things easier you can also make an executable exe-file. Then you can give it to anyone and they just need to press it to start interpret it.
See more about that here: 20. Linking newLISP source and executable – newLisp Users Manual and Reference
Background
Interpreted or compiled implementation
Compiling is when we are translating from one language to another, most of the time we do it from a high level language, i.e. source code only understand by humans to a lower level language, i.e. object code understand by computers. During the translation we get error messages if we done something wrong, then we need to correct it and start over again.
Some languages we use to compile the code: C/C++, C#, Java, ADA, etc.
It’s faster to run a compiled program than to interpret programs, but to compile a program is slow.
A Interpret is a program running other programs.
Some languages often with interpretadted implementations: LISP, JavaScript, etc.
… Working here as you can se ^^ just thinking…
Programming paradigms
Functional programming
Object-oriented programming
Flow-driven programming
Imperative programming
Structured programming
…
LISP – history
John McCarty conceived the LISt Processing language a long time ago around 1956. The language is famous for it’s artificial intelligence applications witch was what it mainly was created for. Today LISP is a lot more.
Basic newLisp
Arithmetic
Notation:
>Written expression
[Explanation]
Result
>(+ 4 5)
[4 + 5 = 9]
9
>(+ 5 7 8 9)
[5 + 7 + 8 + 9 and (+ 5 (+ 7 (+ 8 (+ 9))) = 29]
29
>(- 2)
[2 * -1 = -2]
-2
>(- 8 3)
[8 – 3 = 5]
5
>(- 9 3 2)
[9 – 3 – 2 and (- 9 (- 3 (- 2)) = 4]
4
>(* 2 3)
[2 * 3]
6
>(* 4 5 6)
[4 * 5 * 6 and (* 4 (* 5 (* 6))) = 120]
120
>(/ 10 2)
[10 / 2 ; i några lisp dialekter kan tal representeras som bråk,
då förkortas bråket vid division, andra lisp varianter
implementerar inte detta exakta sätt att utföra division testa alltid.]
5
>(pow 4 3)
[4^3 = 43 , this function is often called expt instead of pow]
64
Any number in the abow examples can be changed to an expression insted.
>(* (+ 4 5) 2 3)
[(4 + 5) * 2 * 3]
54
>(* 4 (+ 3 6 3) (- (* 2 (- 4)) 6))
[4 * (3 + 6 + 3) * (2 * (-4) – 6)]
-672
>(+ 8 (/ (* 2 3) (+ 1 (- 9 (pow 2 3)))))
[8 + ((2 * 3) / (1 + 9 – (2^3)))]
11
We take a look on how the last one evolves:
- (+ 8 (/ (* 2 3) (+ 1 (- 9 (pow 2 3)))))
- (+ 8 (/ (* 2 3) (+ 1 (- 9 8 ))))
- (+ 8 (/ (* 2 3) (+ 1 1)))
- (+ 8 (/ (* 2 3) 2))
- (+ 8 (/ 6 2))
- (+ 8 3)
- 11
Functions
Very easy example to add two numbers:
(define (add t1 t2)
(+ t1 t2))
>(add 45 56)
101
And here is a example for calculate the sum for n2 wher n goes from 1 to levels.
(define (sum-form-squares levels)
(/ (* levels (+ levels 1) (+ (* 2 levels) 1)) 6 ))
>(sum-form-squares 10)
385
Just to explain i can say that 385 = 102 + 92 + 82 + 72 + 62 + 52 + 42 + 32 + 22 + 12
Factorial
As you can see a good application of Lisp is to define functions for mathematical formulas.
It is time for a recursive function to calculate the math expression n!.
A recursive function is a function that in it’s definition makes a call to itself, directly or by going through other functions creating a loop behaviour.
This is both a recursive procedure and a recursive process, later we will look at a recursive procedure with a iterative process the difference is then how the process evolves.
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
>(factorial 7)
[7 * 6 * 5 * 4 * 3 * 2 * 1 ; is writen 7!]
5040
We take a look on how 3! evolves:
- (factorial 3)
- (if (= 3 0) 1 (* 3 (factorial (- 3 1))))
- (* 3 (factorial (- 3 1)))
- (* 3 (factorial 2))
- (* 3 (if (= 2 0) 1 (* 2 (factorial (- 2 1)))))
- (* 3 (* 2 (factorial (- 2 1))))
- (* 3 (* 2 (factorial 1)))
- (* 3 (* 2 (if (= 1 0) 1 (* 1 (factorial (- 1 1))))))
- (* 3 (* 2 (* 1 (factorial (- 1 1)))))
- (* 3 (* 2 (* 1 (factorial 0))))
- (* 3 (* 2 (* 1 (if (= 0 0) 1 (* 0 (factorial (- 0 1)))))))
- (* 3 (* 2 (* 1 1)))
- (* 3 (* 2 1))
- (* 3 2)
- 6
Recursion is very good to understand, it’s one of the important things about functional programming. You can use the knowledge in a lot of other areas, one example:
javascript: function factorial(n)
{if(n==0) return 1; else return n*factorial(n-1);
factorial(7);
This is a JavaScript pseudo-URL, put it on one row in a JavaScript enabeld browsers address bar and hit enter. See the only differences is the syntax. Magic.
We can also use the function in other functions:
(define (permutation n r)
(/ (factorial n)
(factorial (- n r))))
>(permutation 10 5)
[10!/((10 – 5)!)]
30240
(define (combination n r)
(/ (permutation n r)
(factorial r)))
>(combination 10 4)
[10!/((10 – 4)! * 4!) ; we are using the permutation function]
210
Now we do the factorial function as a recursive procedure with a iterative process, I choose to do it with two functions to not introduce something new:
(define (factorial n)
(factorial-inside n 1))
(define (factorial-inside n a)
(if (= n 0)
a
(factorial-inside (- n 1) (* n a))))
>(factorial 9)
362880
We take a new look on how 3! evolves, compare with the earlier one:
- (factorial 3)
- (factorial-inside 3 1)
- (if (= 3 0) 1 (factorial-inside (- 3 1) (* 3 1)))
- (factorial-inside (- 3 1) (* 3 1))
- (factorial-inside 2 3)
- (if (= 2 0) 3 (factorial-inside (- 2 1) (* 2 3)))
- (factorial-inside (- 2 1) (* 2 3))
- (factorial-inside 1 6)
- (if (= 1 0) 6 (factorial-inside (- 1 1) (* 1 6)))
- (factorial-inside (- 1 1) (* 1 6))
- (factorial-inside 0 6)
- (if (= 0 0) 6 (factorial-inside (- 0 1) (* 0 6)))
- 6
And with some new things:
(set ‘a 1)
(define (factorial n)
(if (= n 0)
a
(let (a (* n a))
(factorial (- n 1)))))
>(factorial 6)
720
And here is a non recursive variant.
(define (factorial n)
(if (= n 0)
1
(begin
(set ‘a 1)
(for (i 1 n 1) (set ‘a (* i a))))))
>(factorial 12)
479001600
Fibonacci 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 etc.
(define (fibonacci n)
(if (< n 2)
1
(+ (fibonacci (- n 1))
(fibonacci (- n 2)))))
>(fibonacci 6)
13
Set theory
Graphics – TK
…TCL/TK…
Useful examples
Webb browser
(get-url “url”)(print (replace “]*>” (get-url (read-line)) “” 0))
Webb server
…
Sources – good things to read
- http://www.newlisp.org/ – The home of newLISP.
- http://mitpress.mit.edu/sicp/ – Structure and Interpretation of Computer Programs.
- http://www.gigamonkeys.com/book/ – Practical Common Lisp.
- http://www.alu.org/ – Association of Lisp Users, se the wiki to.
- http://en.wikipedia.org/wiki/NewLISP – newLISP on Wikipedia, se the LISP page to.
Copyright note
This page is here for comments suppose only. Code samples are free to use, the other text is however copyrighted, I don’t want it to spread widely because it may have bad quality. When this document is finished it will probably be under a free license.
June 14, 2006 at 4:06 pm
good stuff – keep it updated… 😉
(so much to do, so little time….)
June 15, 2006 at 12:12 am
Thanks,
Today I added the text in the Working flow part.
More will come 🙂
June 16, 2006 at 10:54 am
This page starts to get a little long; soon I need to add a table of contents I think.
June 28, 2006 at 11:29 am
June 28, 2006 at 6:43 pm
I´m looking for manual of new lisp tk, because i can not fount how to use the tk graphics. But, there is none on the net. So, maybe will be fine that you publish something about.
Tanke you!
July 3, 2006 at 5:33 pm
I have finded not a manual fot TK graphics, but can be very usefull. I want to give you that place. Is in French but undesrtable.-
Click to access 1997-LISI-97002.pdf
July 13, 2006 at 3:25 pm
Information about newLisp-tk you find here: http://newlisp.org/download/newlisp-tk.html but i think you can use any sorce about TCL/TK to learn using TK graphics. I will probably write about this in the future.
September 11, 2006 at 9:04 pm
Newtonraphson & simplexmetoden
June 30, 2007 at 9:10 pm
Test myfunction comment