LISP Assignment (preceded by helpful information about LISP)
The comment operator in LISP is a semi-colon which indicates that all of the following characters on the line should be ignored
The read/eval/print loop repeatedly reads LISP read/eval/print loop expressions typed in interactively, evaluates them and prints their results to the screen
The quote operator ' prevents the evaluation of what follows it.
Here are some LISP functions with what they do
car |
returns the first element in a list |
cdr |
returns everything but the first element in a list, as a list |
|
|
setf |
returns a variable with a value assigned to it |
length |
returns the length of a list |
reverse |
returns the list it was given with its top level elements reversed |
list |
returns a list of its arguments |
listp |
returns t or nil depending on whether its argument is a list or not |
atom |
returns t or nil depending on whether its argument is an atom or not |
zerop |
returns t or nil depending on whether its argument is zero or not |
numberp |
returns t or nil depending on whether its argument is a number or not |
symbolp |
returns t or nil depending on whether its argument is a symbol or not |
null |
returns t or nil depending on whether its argument is nil or not |
equal |
returns t or nil depending on whether its arguments are structurally equal |
= |
returns t or nil depending on whether its arguments are equal or not |
> |
returns t or nil depending on whether its first argument is greater than its second argument or not |
< |
returns t or nil depending on whether its first argument is less than its second argument or not |
/= |
returns t or nil depending on whether its arguments are not equal or not not equal |
>= |
returns t or nil depending on whether its first argument is greater than or equal to its second argument or not |
<= |
returns t or nil depending on whether its first argument is less than or equal to its second argument or not |
member |
returns t or nil depending on whether the first argument is a member of the list which is the second argument |
defun |
returns a function definition |
+ |
returns the sum of its n arguments |
- |
returns the difference of its n arguments |
* |
returns the product of its n arguments |
/ |
returns the quotient of its n arguments (as an integer or as a fraction) |
|
|
truncate |
returns the truncated result of division when the first argument is divided by the second OR returns the truncated result of its single value |
round |
returns the rounded result of division when the first argument is divided by the second OR returns the rounded result of its single value |
append |
returns a list which is the concatenation of its argument lists |
cons |
returns a list with its first argument in front of the second argument which is a list |
max |
returns the maximum number among its arguments |
min |
returns the minimum number among its arguments |
log |
returns the logarithm of its single numeric argument |
abs |
returns the absolute value of its single numeric argument |
rem |
returns the remainder when its first numeric argument is divided by its second |
random |
returns a random number between 0 and its argument |
gcd |
returns the greatest common divisor of its argument |
exp |
returns e raised to its argument power |
expt |
returns the first argument raised to the second argument power |
sqrt |
returns the square root of its argument |
cond |
takes an arbitrary number of clauses containing a test expression and a return expression. It returns the value of the return expression of the first clause whose test expression evaluates to t When writing a function it is customary to make the test expression of the last clause t itself If none of the test expressions evaluates to t then the result of the cond is nil |
not |
returns nil if its argument evaluates to t OR returns t if its argument evaluates to nil |
and |
returns nil if any of its arguments evaluates to nil OR returns the value of its last argument if none evaluate to nil NOTE: and is a short circuit operator |
or |
returns t if any of its argument evaluates to t OR returns the value of its last argument if none evaluate to true NOTE: or is a short circuit operator |
Your LISP assignment is to write two functions using defun (see an example on the handout we used in class yesterday).
Deliverables for each function:
A printout of the function
Printouts of screen captures of the results of testing the function
Disk containing the code for the function.
Note that your code should be complete with heading and comments as usual
Each function's printout should be on a separate page
First function:
The roots of a second-degree polynomial (represented as ax2 + bx + c) can be computed using the quadratic equation (-b + √b2- 4ac) /2a and (-b - √b2- 4ac) /2a
Write a function named quadratic that takes 3 arguments ( a, b, and c) and performs the computation described by the quadratic equation using on the + part of the equation. You may assume that b2-4ac will never be less than zero. Recall that the square root function is sqrt and that - may take zero or more arguments. Note: in the equations above, the square root sign covers
the entire expression b2-4ac not just the b.
If you test your function as (quadratic 1 1 -6) the result should come back 2.0
Second function:
LISP has a built-in function length which tells the number of top level arguments in a list. You are to assume that you have a strange version of list which does not have the built in function. You are to write a recursive function named mylength which produces the same results as the built in function length.
If you test your function as (mylength () ) the result should be 0
If you test your function as (mylength '( how many elements are in this list)) the result should be 7