Applicative Programming in Lisp
Usually, the input to a Lisp function is some sort of data.
Functions are data too, so applicative programming takes this idea and uses Lisp functions in the same way other data: as inputs to other functions and functions returned as values. Today's Programming Lisp Tutorial introduces Applicative Programming in Lisp, MAPCAR and LAMBDA.
And of course we have the answer to the Lisp Programming Challenge from our last post.
Showing posts with label lisp tutorial. Show all posts
Showing posts with label lisp tutorial. Show all posts
Saturday, 22 May 2010
Sunday, 16 May 2010
Yet More Recursion in Lisp
Our last Lisp tutorial looked at using recursion to access data from nested lists. Today's Lisp tutorial looks at more Lisp recursion techniques. Firstly let's look at the Lisp Programming Challenge from our last Lisp recursion post:
(defun get-orbit (planet-list)
(if (null planet-list) nil
(cons (second (first planet-list))
(get-orbit (rest planet-list)))))
(defun get-diameter (planet-list)
(if (null planet-list) nil
(cons (third (first planet-list))
(get-diameter (rest planet-list)))))
(defun get-mass (planet-list)
(if (null planet-list) nil
(cons (fourth (first planet-list))
(get-mass (rest planet-list)))))
More Recursion in Lisp
There are certain functions that we usually apply to numbers and cannot apply to lists for example:
(/ 8 2)
4
Whereas the following will create an error:
(/9 '(2 3 4))
If, as in the above example, we wanted to divide a list of numbers by another number we could write a recursive function to do this:
(defun divide-list-by-x (the-list x)
(if (null the-list) nil
(cons (/ (first the-list) x)
(divide-list-by-x (rest the-list) x))))
(divide-list-by-x '(2 3 4) 2)
(1 3/2 2)
Similarly we could write a recursive Lisp function to perform the modulus function on a list of numbers:
(defun list-mod (the-list mod)
(if (null the-list) nil
(cons (mod (first the-list) mod)
(list-mod (rest the-list) mod))))
When writing recursive Lisp functions we need to be able to define the following 3 things:
* When to stop recursing
* How do we take the next step
* How to call the recursive function
The two recursive Lisp examples above use a single test to identify when to stop recursing, however we can also use two (or more) tests to identify when the recursive function should stop. In this example, we check to see if a given number is less than every number in a list by writing a recursive function. The function stops recursing if it reaches the end of the list, or if the function encounters a number that is equal to or greater than our test number:
(defun less-than-list (x the-list)
(cond ((null the-list) t)
((>= x (first the-list)) nil)
(t (less-than-list x (rest the-list)))))
Today's Programming Lisp Challenge
Lisp has many different data types. Write a function to extract the numbers from this list:
(1 Partridge 2 Turtle Doves 3 French Hens 4 Calling Birds 5 Gold Rings)
So:
(extract-numbers '(1 Partridge 2 Turtle Doves 3 French Hens 4 Calling Birds 5 Gold Rings))
should return:
(1 2 3 4 5)
Subscribe to the RSS feed and check back soon for the answer and more Programming Lisp tutorials.
(defun get-orbit (planet-list)
(if (null planet-list) nil
(cons (second (first planet-list))
(get-orbit (rest planet-list)))))
(defun get-diameter (planet-list)
(if (null planet-list) nil
(cons (third (first planet-list))
(get-diameter (rest planet-list)))))
(defun get-mass (planet-list)
(if (null planet-list) nil
(cons (fourth (first planet-list))
(get-mass (rest planet-list)))))
More Recursion in Lisp
There are certain functions that we usually apply to numbers and cannot apply to lists for example:
(/ 8 2)
4
Whereas the following will create an error:
(/9 '(2 3 4))
If, as in the above example, we wanted to divide a list of numbers by another number we could write a recursive function to do this:
(defun divide-list-by-x (the-list x)
(if (null the-list) nil
(cons (/ (first the-list) x)
(divide-list-by-x (rest the-list) x))))
(divide-list-by-x '(2 3 4) 2)
(1 3/2 2)
Similarly we could write a recursive Lisp function to perform the modulus function on a list of numbers:
(defun list-mod (the-list mod)
(if (null the-list) nil
(cons (mod (first the-list) mod)
(list-mod (rest the-list) mod))))
When writing recursive Lisp functions we need to be able to define the following 3 things:
* When to stop recursing
* How do we take the next step
* How to call the recursive function
The two recursive Lisp examples above use a single test to identify when to stop recursing, however we can also use two (or more) tests to identify when the recursive function should stop. In this example, we check to see if a given number is less than every number in a list by writing a recursive function. The function stops recursing if it reaches the end of the list, or if the function encounters a number that is equal to or greater than our test number:
(defun less-than-list (x the-list)
(cond ((null the-list) t)
((>= x (first the-list)) nil)
(t (less-than-list x (rest the-list)))))
Today's Programming Lisp Challenge
Lisp has many different data types. Write a function to extract the numbers from this list:
(1 Partridge 2 Turtle Doves 3 French Hens 4 Calling Birds 5 Gold Rings)
So:
(extract-numbers '(1 Partridge 2 Turtle Doves 3 French Hens 4 Calling Birds 5 Gold Rings))
should return:
(1 2 3 4 5)
Subscribe to the RSS feed and check back soon for the answer and more Programming Lisp tutorials.
Sunday, 25 April 2010
Using AND NOT and OR in Lisp
Today's Lisp tutorial looks at using AND NOT and OR in Lisp, but firstly let's have a look at the answer to the challenge set in our last Lisp tutorial Using LET in Lisp:
Write a Lisp function called GUESS, where you try and guess the number thrown by a dice, this will have several parts.This function will randomly choose from 6 possibilities. You call the function with your guess of 1, 2, 3, 4 5 or 6, the function randomly chooses 6 choices and tells you if your guess was right or wrong.
(defun guess (n)
(let ((dice (+ 1 (random 6))))
(cond ((equalp dice n ) (list 'you 'guessed 'right 'it 'was dice))
(t (list 'you 'guessed 'wrong 'it 'was dice)))))
The NOT Predicate in Lisp
We've looked at predicates in Lisp in a previous Lisp tutorial post. Predicates return T or NIL if the statement is true or false. In Lisp T is true and NIL is false. Using NOT turns T into NIL and NIL into T.
For example using the ZEROP predicate we can test if the number is zero:
(zerop 4)
NIL
(zerop 0)
T
Using NOT we can check to see if a number is not zero:
(not (zerop 0))
NIL
The AND Predicate in Lisp
The AND Predicate in Lisp takes two inputs, both of these must be true for it to evaluate and return T otherwise it returns NIL.
(and (> 6 4) (< 1 3))
T
Here only one of our predicates is true:
(and (numberp 6) (equalp 1 9))
NIL
The OR Predicate in Lisp
T
Today's Lisp Challenge
Write a function called BETWEEN that takes 3 input arguments N, MIN and MAX. This function tests to see if N is between MIN and MAX.
Subscribe to Programming Lisp Tutorial Blog and check back to see the solution!
Write a Lisp function called GUESS, where you try and guess the number thrown by a dice, this will have several parts.This function will randomly choose from 6 possibilities. You call the function with your guess of 1, 2, 3, 4 5 or 6, the function randomly chooses 6 choices and tells you if your guess was right or wrong.
(let ((dice (+ 1 (random 6))))
(cond ((equalp dice n ) (list 'you 'guessed 'right 'it 'was dice))
(t (list 'you 'guessed 'wrong 'it 'was dice)))))
The NOT Predicate in Lisp
We've looked at predicates in Lisp in a previous Lisp tutorial post. Predicates return T or NIL if the statement is true or false. In Lisp T is true and NIL is false. Using NOT turns T into NIL and NIL into T.
For example using the ZEROP predicate we can test if the number is zero:
(zerop 4)
NIL
(zerop 0)
T
Using NOT we can check to see if a number is not zero:
(not (zerop 4))
T
T
NIL
The AND Predicate in Lisp
The AND Predicate in Lisp takes two inputs, both of these must be true for it to evaluate and return T otherwise it returns NIL.
(and (> 6 4) (< 1 3))
T
Here only one of our predicates is true:
(and (numberp 6) (equalp 1 9))
NIL
The OR Predicate in Lisp
The OR Predicate in Lisp returns T if either one or both arguments are true:
(or (numberp 6) (equalp 1 9))T
(or (> 7 6) (equalp 1 1))
T
T
Today's Lisp Challenge
Write a function called BETWEEN that takes 3 input arguments N, MIN and MAX. This function tests to see if N is between MIN and MAX.
Subscribe to Programming Lisp Tutorial Blog and check back to see the solution!
Thursday, 22 April 2010
Using LET in LISP
Today we're looking at using LET, but firstly let's look at the answer to our last Lisp tutorial: Write a function called MY-COMPARE using COND that will take two numbers as input and return one of these statements as appropriate:
THE FIRST NUMBER IS LARGER
THE SECOND NUMBER IS LARGER
THE TWO NUMBERS ARE EQUAL
THE FIRST NUMBER IS LARGER
THE SECOND NUMBER IS LARGER
THE TWO NUMBERS ARE EQUAL
(defun my-compare (x y)
(cond ((> x y) '(the first number is larger))
((< x y) '(the second number is larger))
(t '(the two numbers are equal))))
(cond ((> x y) '(the first number is larger))
((< x y) '(the second number is larger))
(t '(the two numbers are equal))))
Using Let in Lisp
Let creates a local variable: a variable that is only valid inside the function. One common use is to avoid performing the same function over and over again. For example if we need the square of a number several times, we could calculate it once and give it a name rather than calculating it every time:
(defun my-square (x)
(let ((the-square (* x x)))
(list 'the 'square 'of x ’is the-square x 'multiplied 'by x 'is the-square)))
One of the best ways to learn is to look at lots of examples. Here's another example that uses LET and calculates the average of two numbers:
(defun average (x y)
(let ((sum (+ x y)))
(list x y ’average ’is (/ sum 2.0))))
(let ((sum (+ x y)))
(list x y ’average ’is (/ sum 2.0))))
So far our examples have only used one local variable, we can use LET to specify multiple local variables:
(defun sum-and-difference (x y)
(let ((sum (+ x y))
(difference (- x y)))
(list 'the 'sum 'of x 'and y 'is sum 'the 'difference 'is difference)))
This Lisp let example asks the user to enter a number and uses the NUMBERP predicate to check that what has been entered is a number. If the answer is a number it returns val (which is the number), if NUMBERP returns NIL then the asknumber function is called again.
(defun ask-number ()
(format t "Please enter a number. ")
(let ((val (read)))
(if (numberp val)
val
(ask-number))))
(let ((sum (+ x y))
(difference (- x y)))
(list 'the 'sum 'of x 'and y 'is sum 'the 'difference 'is difference)))
This Lisp let example asks the user to enter a number and uses the NUMBERP predicate to check that what has been entered is a number. If the answer is a number it returns val (which is the number), if NUMBERP returns NIL then the asknumber function is called again.
(defun ask-number ()
(format t "Please enter a number. ")
(let ((val (read)))
(if (numberp val)
val
(ask-number))))
Here is the general form of LET in Lisp:
(LET ((var-1 value-1)
(var-2 value-2)
...
(var-n value-n))
body)
(var-2 value-2)
...
(var-n value-n))
body)
LET* in Lisp
The LET* creates the local variables one at a time rather than all at once. This means that one local variable can refer to another.
(defun price-change (old new)
(let* ((diff (- new old))
(proportion (/ diff old))
(percentage (* proportion 100.0)))
(list ’widgets ’changed ’by percentage ’percent)))
Summary
Using LET and LET* functions in Lisp allow us to create local variables.
Today's Lisp Challenge
Write a Lisp function called GUESS, where you try and guess the number thrown by a dice, this will have several parts.This function will randomly choose from 6 possibilities. You call the function with your guess of 1, 2, 3, 4 5 or 6, the function randomly chooses 6 choices and tells you if your guess was right or wrong.
There are several steps to solving this problem. Any time you have a programming challenge with several steps, break it down into the individual tasks and get these working one by one. Make divide and conquer your programming mantra!
1. The RANDOM function in Lisp returns a random number between 0 and n-1. For example
(random 2) returns one of two possibilities, either 0 or 1. Try evaluating the RANDOM function in your version of Common Lisp.
2. Start writing your GUESS function, by getting it to return either 0, 1, 2, 3, 4 or 5. Then add 1 to the result to get the numbers one to 6.
3. Use LET to store this random result as a local variable called dice.
3. Use COND to check whether the guess of matches the result ofour local variable dice.
4. If the guess is right return: YOU GUESSED RIGHT IT WAS [put the local variable dice here]
5. If the guess was wrong return: YOU GUESSED WRONG IT WAS [put the local variable dice here]
(defun price-change (old new)
(let* ((diff (- new old))
(proportion (/ diff old))
(percentage (* proportion 100.0)))
(list ’widgets ’changed ’by percentage ’percent)))
Summary
Using LET and LET* functions in Lisp allow us to create local variables.
Today's Lisp Challenge
Write a Lisp function called GUESS, where you try and guess the number thrown by a dice, this will have several parts.This function will randomly choose from 6 possibilities. You call the function with your guess of 1, 2, 3, 4 5 or 6, the function randomly chooses 6 choices and tells you if your guess was right or wrong.
There are several steps to solving this problem. Any time you have a programming challenge with several steps, break it down into the individual tasks and get these working one by one. Make divide and conquer your programming mantra!
1. The RANDOM function in Lisp returns a random number between 0 and n-1. For example
(random 2) returns one of two possibilities, either 0 or 1. Try evaluating the RANDOM function in your version of Common Lisp.
2. Start writing your GUESS function, by getting it to return either 0, 1, 2, 3, 4 or 5. Then add 1 to the result to get the numbers one to 6.
3. Use LET to store this random result as a local variable called dice.
3. Use COND to check whether the guess of matches the result ofour local variable dice.
4. If the guess is right return: YOU GUESSED RIGHT IT WAS [put the local variable dice here]
5. If the guess was wrong return: YOU GUESSED WRONG IT WAS [put the local variable dice here]
There are a few steps involved in this, get them working one by one and check back tomorrow for the answer and more programming Lisp tutorials.
Saturday, 17 April 2010
Using COND in Common Lisp
Our last post looked at IF THEN statements using the IF function in Lisp. Here's the answer to the challenge set in that post, to write a MY-ABSOLUTE function using an IF function:
(defun my-absolute (x)
(if (>= x 0) x
(* -1 x)))
(if (>= x 0) x
(* -1 x)))
COND
It's possible to use multiple IF functions but if you have lots of things to test it can be more convenient to use a COND function.
COND is a conditional function. It consists of any number of
test and consequent clauses.
(COND (first-test first-consequent)
(second-test second-consequent)
....
(last-test last-consequent))
COND works by progressing through each clause in turn. If the test part is true, COND evaluates the consequent part and returns its value, it does not evaluate any further clauses.
If the test evaluates to NIL (false), COND jumps to the next clause. If all clauses are false, COND returns NIL.
COND is a conditional function. It consists of any number of
test and consequent clauses.
(COND (first-test first-consequent)
(second-test second-consequent)
....
(last-test last-consequent))
COND works by progressing through each clause in turn. If the test part is true, COND evaluates the consequent part and returns its value, it does not evaluate any further clauses.
If the test evaluates to NIL (false), COND jumps to the next clause. If all clauses are false, COND returns NIL.
Here's an example COND function:
(defun what-is (x)
(cond ((equal x ’apple) ’fruit)
((equal x ’asparagus) ’vegetable)
((equal x ’pork-chop) ’meat)
(t ’unknown)))
As shown in the example above, it's useful to put a T as the last COND clause. This allows you to return a value or perform another task if all of the preceeding clauses are false.
Today's Challenge:
Write a function called MY-COMPARE using COND that will take two numbers as input and return one of these statements as appropriate:
THE FIRST NUMBER IS LARGER
THE SECOND NUMBER IS LARGER
THE TWO NUMBERS ARE EQUAL
Monday, 5 April 2010
Lists in Lisp
Lists are are one of Lisp's most flexible and powerful data types. Here are some examples of lists:
(red orange yellow green blue indigo violet)
(10 green bottles)
(9 18 27 36 45 54 63 72 81 90 99 108)
(thisisalist)
((a)(list of) (lists))
length '(red orange yellow green blue indigo violet))
7
(length '(10 green bottles))
3
(length '(9 18 27 36 45 54 63 72 81 90 99 108))
12
(length '(thisisalist))
1
(length '((a)(list of) (lists)))
3
As you can see from the final example, lists can contain other lists. The elements of the list are the things that appear inside only one level of parentheses, so in the final example the LENGTH function returns 3 because there are only 3 elements to the list:
(a)
(list of)
(lists)
As we saw briefly in the first steps with Lisp post we can extract individual elements from lists using these functions:
(first '(a b c d e))
A
(second '(a b c d e))
B
(third '(a b c d e))
C
(rest '(a b c d e))
(B C D E)
(last '(a b c d e))
(E)
A
(second '(a b c d e))
B
(third '(a b c d e))
C
(rest '(a b c d e))
(B C D E)
(last '(a b c d e))
(E)
(first (first '((a)(list of) (lists))))
A Creating a new list is very easy using the LIST function:
(list 'banana)
(banana) (list 'water 'malt 'hops 'yeast)
(WATER MALT HOPS YEAST)
Again we have to put the quote mark ' in front of each word here, otherwise Lisp would try to evaluate or understand what we mean by water, malt, hops and yeast i.e. had we assigned values to these.
We'll look more at using, creating and extracting data from lists in future posts.
Wednesday, 24 March 2010
Using Cons To Create Lists In Lisp
Today's post looks at creating lists in Lisp. Before we do that let's have a look at the answer to yesterday's post:
Write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.
(defun square-odd-p (x)
(oddp (* x x)))
Creating Lists in Lisp
cons is a Common Lisp primitive, that creates lists from an atom
(number or letter) and a list:
(cons 1 '(2 3))
(1 2 3)
We'll use cons to help us write a function to replace the first member of a list. In order to do this we'll:
(cons new-item (rest the-list)))
And an example:
(replace-first 'peter-pan '(one flew over the cuckoos nest))
(PETER-PAN FLEW OVER THE CUCKOOS NEST)
In the next post we'll look at some more examples of building and manipulating lists.
Write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.
(defun square-odd-p (x)
(oddp (* x x)))
Creating Lists in Lisp
cons is a Common Lisp primitive, that creates lists from an atom
(number or letter) and a list:
(cons 1 '(2 3))
(1 2 3)
We'll use cons to help us write a function to replace the first member of a list. In order to do this we'll:
- Have to take two inputs to our function: the new item and the list
- We'll use the rest function to return everything but the first member of the list
- We can then cons our new item to the start of this list:
(cons new-item (rest the-list)))
And an example:
(replace-first 'peter-pan '(one flew over the cuckoos nest))
(PETER-PAN FLEW OVER THE CUCKOOS NEST)
In the next post we'll look at some more examples of building and manipulating lists.
Tuesday, 23 March 2010
Predicates In Lisp
Today we're looking at predicates. Predicates are tests that we can use to see if something is true or false.
Before we do that let's look at the answers from yesterday:
(defun minus-one (x)
(- x 1))
(defun ten-times-bigger (x)
(* x 10))
(defun weeks-to-days (weeks)
(* weeks 7))
It doesn't matter if you called weeks x or something entirely different, it is often useful to give our arguments names that are useful.
Predicates
As mentioned earlier predicates are tests that return either True or False. In lisp:
T = True
Nil = False
Here are some examples:
oddp checks to see if a number is odd:
(oddp 5)
T
(oddp 6)
NIL
evenp checks to see if a number is even:
(evenp 5)
NIL
(evenp 6)
T
Here are some other common predicates with examples:
(zerop 0)
T
(= 3 4)
NIL
(> 3 4)
NIL
(< 5 10)
T
Predicate Summary
In yesterday's post we wrote our own functions. We can include predicates in these functions. For example, this function adds two numbers together and checks to see if the result is even, again we could all it almost anything but as it's a predicate that check to see if the sum of two numbers is even we'll call it sum-even-p:
(defun sum-even-p (x y)
(evenp (+ x y)))
Notice the order of the function
(evenp (+ x y)))
the input element to evenp is the + function and x y
Today's Challenge
Today's challenge write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.
Check tomorrow's post for the answer and more Lisp tutorials.
Before we do that let's look at the answers from yesterday:
(defun minus-one (x)
(- x 1))
(defun ten-times-bigger (x)
(* x 10))
(defun weeks-to-days (weeks)
(* weeks 7))
It doesn't matter if you called weeks x or something entirely different, it is often useful to give our arguments names that are useful.
Predicates
As mentioned earlier predicates are tests that return either True or False. In lisp:
T = True
Nil = False
Here are some examples:
oddp checks to see if a number is odd:
(oddp 5)
T
(oddp 6)
NIL
evenp checks to see if a number is even:
(evenp 5)
NIL
(evenp 6)
T
Here are some other common predicates with examples:
(zerop 0)
T
(= 3 4)
NIL
(> 3 4)
NIL
(< 5 10)
T
Predicate Summary
- (evenp n) true if n is even
- (floatp n) true if n is a floating point number
- (integerp n) true if n is an integer
- (minusp n) true if n is less than zero
- (numberp n) true if n is a number
- (oddp n) true if n is an odd number (0 is considered even)
- (plusp n) true if n is greater than 0
- (zerop n) true if n is 0
- (> x y) true if x is greater than y
- (< x y) true if x is less than y
- (>= x y) true if x is greater than or equal to y
- (<= x y) true if x is less than or equal to y
In yesterday's post we wrote our own functions. We can include predicates in these functions. For example, this function adds two numbers together and checks to see if the result is even, again we could all it almost anything but as it's a predicate that check to see if the sum of two numbers is even we'll call it sum-even-p:
(defun sum-even-p (x y)
(evenp (+ x y)))
Notice the order of the function
(evenp (+ x y)))
the input element to evenp is the + function and x y
Today's Challenge
Today's challenge write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.
Check tomorrow's post for the answer and more Lisp tutorials.
Monday, 22 March 2010
Writing A Function In Lisp
There are a large number of built in functions in Lisp, it's also possible, useful and very easy to write your own functions in Lisp. Have a look at this function:
(defun my-square (x)
(* x x))
Let's break this down:
defun is a function used to define new functions.
my-square is the name that we've given to our function, we could call it almost anything we like but it makes to give it a useful name so that we can remember it later.
x is the argument or input to our function. Again we could call this almost anything we like.
(* x x) This is where the actual work of our function takes place, we using the multiply function and multiplying x by itself.
When you have written and evaluated this function, it can be called and used just any other function:
(my-square 5)
25
(my-square 9)
81
It's good practice to comment your function, both so that others can understand it quickly and easily and for yourself.
(defun my-square (x)
"this function squares an input number x"
(* x x))
Using double quotes " " adds a definition to your function, these can only be used after you have defined the input arguments.
A more versatile way of adding comments that can be used anywhere is to use semicolons, anything after the semicolon is ignored:
(defun my-square (x)
"this function squares an input number x"
(* x x)) ; this line squares x
We'll work more on writing new functions tomorrow, for now see if you can write these functions, answers in tomorrow's post:
minus-one a function that takes a single number as input and returns the number that is one less
ten-times-bigger a function that takes a single number as input and returns the number that is ten times bigger
weeks-to-days a function that takes a number of weeks as input and returns the total number of equivalent days
These examples may seem simple, however this is a really good stepping stone to learn how to build much more powerful functions in Lisp. Answers and more on building functions tomorrow...
(defun my-square (x)
(* x x))
Let's break this down:
defun is a function used to define new functions.
my-square is the name that we've given to our function, we could call it almost anything we like but it makes to give it a useful name so that we can remember it later.
x is the argument or input to our function. Again we could call this almost anything we like.
(* x x) This is where the actual work of our function takes place, we using the multiply function and multiplying x by itself.
When you have written and evaluated this function, it can be called and used just any other function:
(my-square 5)
25
(my-square 9)
81
It's good practice to comment your function, both so that others can understand it quickly and easily and for yourself.
(defun my-square (x)
"this function squares an input number x"
(* x x))
Using double quotes " " adds a definition to your function, these can only be used after you have defined the input arguments.
A more versatile way of adding comments that can be used anywhere is to use semicolons, anything after the semicolon is ignored:
(defun my-square (x)
"this function squares an input number x"
(* x x)) ; this line squares x
We'll work more on writing new functions tomorrow, for now see if you can write these functions, answers in tomorrow's post:
minus-one a function that takes a single number as input and returns the number that is one less
ten-times-bigger a function that takes a single number as input and returns the number that is ten times bigger
weeks-to-days a function that takes a number of weeks as input and returns the total number of equivalent days
These examples may seem simple, however this is a really good stepping stone to learn how to build much more powerful functions in Lisp. Answers and more on building functions tomorrow...
Sunday, 21 March 2010
First Steps With Lisp
Assuming you've followed yesterday's post and installed a version of Common Lisp, today we'll look at some first steps in learning Lisp.
We'll discuss background ideas and other useful theory in future posts, but for now we'll dive straight in and get our hands dirty with some Lisp programming.
1. Open your Lisp listener window, if it's not all ready showing, look for 'Listener' under your window menu.
2. As you would expect, Lisp can perform all sorts of mathematical operations, type this into your listener:
(+ 2 3)
It should of course return 5. There are a couple of useful points here:
-The function always come first
-The function and its arguments are surrounded by ()
3. Here are some more examples:
(+ 2 4 5 6)
17
(* 10 9)
90
(- 10 2.5)
7.5
and so on...
4. These operations can be nested inside one another for example:
(+ 1 (* 3 3))
10
The inner parentheses get evaluated first.
5. Aside from mathematical functions, many function access data. For example:
(first '(a b c d e))
A
6. The function 'first' returns the first item of a list. There are many other similar functions for accessing data:
(second '(a b c d e))
B
(third '(a b c d e))
C
(rest '(a b c d e))
(B C D E)
(last '(a b c d e))
(E)
7. The ' quote mark stops the Lisp from trying to evaluate the list (from trying to interpret what the list means - we could have for example assigned values to these letters).
Tomorrow we'll start building our own functions, but for now get used to using these functions in your version of Lisp
We'll discuss background ideas and other useful theory in future posts, but for now we'll dive straight in and get our hands dirty with some Lisp programming.
1. Open your Lisp listener window, if it's not all ready showing, look for 'Listener' under your window menu.
2. As you would expect, Lisp can perform all sorts of mathematical operations, type this into your listener:
(+ 2 3)
It should of course return 5. There are a couple of useful points here:
-The function always come first
-The function and its arguments are surrounded by ()
3. Here are some more examples:
(+ 2 4 5 6)
17
(* 10 9)
90
(- 10 2.5)
7.5
and so on...
4. These operations can be nested inside one another for example:
(+ 1 (* 3 3))
10
The inner parentheses get evaluated first.
5. Aside from mathematical functions, many function access data. For example:
(first '(a b c d e))
A
6. The function 'first' returns the first item of a list. There are many other similar functions for accessing data:
(second '(a b c d e))
B
(third '(a b c d e))
C
(rest '(a b c d e))
(B C D E)
(last '(a b c d e))
(E)
7. The ' quote mark stops the Lisp from trying to evaluate the list (from trying to interpret what the list means - we could have for example assigned values to these letters).
Tomorrow we'll start building our own functions, but for now get used to using these functions in your version of Lisp
Subscribe to:
Posts (Atom)