Assignment 1
Library file function definitions:
- equalp, eql
- length
- 1+, 1-
- abs
- elt, nth
- first, second, third, cadr, rest
- integerp - hack, always returns true
Problems:
Problem 1:
1. (third (the quick brown fox))
ERROR: Too many arguments. Error signalled by THE.
CAUSE: Missing quote in front of list
SOLUTION:
2. (list 2 and 2 is 4)
ERROR: The variable AND is unbound.
CAUSE: Missing quote around 'and' and 'is'
SOLUTION:
3. (cdr (car '(a b c)))
ERROR: A is not of type LIST. Error signalled by CDR.
CAUSE: cdr needs a list as input
SOLUTION:
4. (+ 1 '(length (list t t t t)))
ERROR: (LENGTH (LIST T T ...)) is not of type NUMBER.
CAUSE: Unnecessary quote before 'length' function.
SOLUTION:
5. (evenp (+ 4.0 2.0))
ERROR: 6.0 is not of type INTEGER.
CAUSE: evenp requires parameters to be integers.
SOLUTION:
6. (cons 'mary (betty carolyn))
ERROR: The function BETTY is undefined.
CAUSE: Missing quote in front of list.
SOLUTION:
7. (append '(a b c) 'd)
ERROR: No error, just incorrect answer (A B C .D)
CAUSE: Missing brackets around 'd': append really wants 2 lists
SOLUTION:
8. (cons 'mary (list betty carolyn))
ERROR: The variable BETTY is unbound.
CAUSE: Missing quotes in front of atoms
SOLUTION:
9. (1+ '(cdr (2 3 4)))
ERROR: (CDR (2 3 4)) is not of type NUMBER.
CAUSE: The function 1+ cannot increment elements of a list b iteself
SOLUTION:
10. (setq x length (a b c))
ERROR: The variable LENGTH is unbound.
CAUSE: Missing backets around length and quote around list
SOLUTION:
PROBLEM 2, Question 1.
Function Name : sub20
Description : Takes a number, subtracts 20 from it, and returns the result.
PROBLEM 2, Question 2.
Function Name : square50-
Description : Returns the square of its argument iff its argument is a
positive integer less than or equal to 50. Else returns nil.
solution using cond
solution using if
PROBLEM 3
Function Name : constrain
Description : If x is less than or equal to minarg, minarg is returned.
If x is greater than or equal to maxarg, maxarg is returned.
Otherwise x is returned.
Notes : As the name implies the implementation uses the conditional
construct.
The specification does not explicitly state the
arguments are numbers but the use of < and > operators implies
so.
Variant Problem 3:
Function name : constrain-if
Description : as for constrain above.
Notes : As for constrain-if above except that as the name implies
this implementation uses the if construct.
PROBLEM 4
Function name : precede
Description : Given an integer x and a list l as arguments, a list of all
the items immediately preceding x in the list l is returned.
Notes : This implementation is recursive.
Does not check any sublists
version using cond
version using if
variant of PROBLEM 4
Function Name : precede-iter
Description : As for precede above.
Notes : As the name implies this implementation is iterative.
check - this may not be correct
PROBLEM 5
Function Name : Intersperse
Description : Takes an object o and a list l as arguments and returns a new; list in which the object appears between every pair of elements
in the original list.
Notes : This is a recursive implementation.
variant PROBLEM 5
Function Name : Intersperse-Iter
Description : As for Intersperse above.
Notes : This is an iterative implementation.
Problem 6
recursive and iterative versions of a function which returns true
iff the difference between every successive pair of elements is 1.