Getting Started with Lisp
Racket
Download and Install Racket
wget http://mirror.racket-lang.org/installers/6.0/racket-6.0-x86_64-linux-ubuntu-precise.sh chmod +x racket-6.0-x86_64-linux-ubuntu-precise.sh ./racket-6.0-x86_64-linux-ubuntu-precise.sh
DrRacket
Launch DrRacket from the path you set-up during installation.
~/racket/bin/drracket
Interactive Shell
Click No language chosen > Choose language to choose a language. Pick The Racket Language option and press OK. Notice that the language is now specified as:
#lang racket
Click Run.
Hello World
"Hello, world!"
Write first function
Let's write an add function that will add a list of numbers together. First, let's look at the built-in add function.
(+ 1 2 3) 6
Lisp is a language of lists. These lists are surrounded by parenthesis. The first item in a lisp list is the name of the function while the remaining items are sent as input or arguments to the function. So + is the method name and the numbers are the parameters. This operation performs the calculation by adding all numbers 1 + 2 + 3
. So a C-like equivalent function would look like:
int add(int num1,int num2,int num3) { ... }
Although, this isn't completely accurate because the + lisp function takes in a list of numbers.
(define (add i . n) (if (null? n) i (apply add (cons (+ i (car n)) (cdr n))))) (add 1 2 3) 6
Clojure
Download and Run
wget https://github.com/clojure/clojure/archive/master.zip wget http://central.maven.org/maven2/org/clojure/clojure/1.6.0/clojure-1.6.0.jar unzip master.zip cp clojure-1.6.0.jar clojure-master/ cd clojure-master java -cp clojure-1.6.0.jar clojure.main
Hello World
(println "Hello, world!")