This page is under construction… RFC

newLISP logo, dragonfly wallpaper by Brian Grayless
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:

  1. (+ 8 (/ (* 2 3) (+ 1 (- 9 (pow 2 3)))))
  2. (+ 8 (/ (* 2 3) (+ 1 (- 9 8 ))))
  3. (+ 8 (/ (* 2 3) (+ 1 1)))
  4. (+ 8 (/ (* 2 3) 2))
  5. (+ 8 (/ 6 2))
  6. (+ 8 3)
  7. 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:

  1. (factorial 3)
  2. (if (= 3 0) 1 (* 3 (factorial (- 3 1))))
  3. (* 3 (factorial (- 3 1)))
  4. (* 3 (factorial 2))
  5. (* 3 (if (= 2 0) 1 (* 2 (factorial (- 2 1)))))
  6. (* 3 (* 2 (factorial (- 2 1))))
  7. (* 3 (* 2 (factorial 1)))
  8. (* 3 (* 2 (if (= 1 0) 1 (* 1 (factorial (- 1 1))))))
  9. (* 3 (* 2 (* 1 (factorial (- 1 1)))))
  10. (* 3 (* 2 (* 1 (factorial 0))))
  11. (* 3 (* 2 (* 1 (if (= 0 0) 1 (* 0 (factorial (- 0 1)))))))
  12. (* 3 (* 2 (* 1 1)))
  13. (* 3 (* 2 1))
  14. (* 3 2)
  15. 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:

  1. (factorial 3)
  2. (factorial-inside 3 1)
  3. (if (= 3 0) 1 (factorial-inside (- 3 1) (* 3 1)))
  4. (factorial-inside (- 3 1) (* 3 1))
  5. (factorial-inside 2 3)
  6. (if (= 2 0) 3 (factorial-inside (- 2 1) (* 2 3)))
  7. (factorial-inside (- 2 1) (* 2 3))
  8. (factorial-inside 1 6)
  9. (if (= 1 0) 6 (factorial-inside (- 1 1) (* 1 6)))
  10. (factorial-inside (- 1 1) (* 1 6))
  11. (factorial-inside 0 6)
  12. (if (= 0 0) 6 (factorial-inside (- 0 1) (* 0 6)))
  13. 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

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.

Advertisements

11 Responses to “newLISP”

  1. newlisper Says:

    good stuff – keep it updated… 😉

    (so much to do, so little time….)

  2. knifstrom Says:

    Thanks,

    Today I added the text in the Working flow part.
    More will come 🙂

  3. knifstrom Says:

    This page starts to get a little long; soon I need to add a table of contents I think.

  4. Demian Says:

    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!

  5. Demian Says:

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

    http://www.lisi.ensma.fr/ftp/pub/documents/reports/1997/1997-LISI-97002.pdf

  6. knifstrom Says:

    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.

  7. jonas Says:

    Newtonraphson & simplexmetoden

  8. TestName Says:

    Test myfunction comment


  9. Spot on with this write-up, I actually believe this website needs a great deal more attention.

    I’ll probably be returning to read through more, thanks for the advice!

  10. www.192.com Says:

    Keeping your body physically fit is really important and can help prevent the occurrence
    of various illnesses later on in life. Many hand tools are automatic and are generally powered by electricity.

    These craftsmen have the knowledge, tools and time to ensure your home.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: