>(marroquin 9) >#<struct:graphic>
(define spiral (lambda (angle len n) (if (= n 0) (adjoin) (adjoin (bend angle) (straight len) (spiral angle (add1 len) (sub1 n))))))Rewrite spiral so that it is tail-recursive. You are free to use any helper-functions you may require.
> (cond->if '(cond ((= x 1) 'foo) ((= x 2) 'bar) (else 'error))) (if (= x 1) (quote foo) (if (= x 2) (quote bar) (quote error))) >You may assume that every cond expression has an else part.
(define foo (lambda (<symbol>*) <tail wrt foo>))
where <symbol>* is zero or more symbols and an expression is <tail wrt foo> if it is not an application of foo:
<contains no foo applications>
(foo <contains no foo applications>*)
where <contains no foo applications> is an expression which contains no applications of foo, and <contains no foo applications>* is zero or more expressions which contain no applications of foo.
Write a function, tail-definition?, which takes an expression, expr, as an argument, and returns #t if the expression is a <tail definition> and #f otherwise. For example:
> (tail-definition? '(define fact (lambda (x acc) (if (= x 0) acc (fact (- x 1) (* x acc)))))) #t > (tail-definition? '(define fact (lambda (x) (if (= x 0) 1 (* (fact (- x 1))))))) #f > (tail-definition? '(define member? (lambda (item ls) (and (not (null? ls)) (or (eq? item (car ls)) (member? item (cdr ls))))))) #t >Hint: Divide and conquer. Write helper-functions, tail-wrt? and no-applications-of?. Both take a function name, fname, and an expression, expr, as arguments. tail-wrt? returns #t if expr is tail with respect to fname and #f otherwise. no-applications-of? returns #t if there are no applications of fname in expr, and #f otherwise.
> (list-all? '(1 ((2 3)) (4)))) #t > (list-all? '(1 ((2 . 3)) (4)))) #fYou may use list? to define list-all?.
* This webpage is located at http://cs.unm.edu/~williams/cs257/homework4s03.html