11-04-2013 Supplement

posted Nov 4, 2013, 8:13 PM by Samuel Konstantinovich   [ updated Nov 4, 2013, 8:13 PM ]
Questions and solutions for quiz:

SumPos:

Write a function that adds up the values in a list of integers, but treats the negatives like positives:

(define (sumpos L)
 (cond
    ( (null? L)      0)
    ( (list? (car L))    (+ (sumpos (car L) ) (sumpos (cdr L)) ) )   ;If the quiz included sublists this was a required line
    ( else  (+ (abs (car L)) (sumpos (cdr L)) ) ) ))

Funky Sequence:  1 5 25 ...  f(n-1) * f(n-3) + f(n-2)

(define (funkyseq n)
   (cond
     (  (= n 0) 1 )   ;a
     (  (= n 1) 5 )   ;b
     (  (= n 2) 25 )  ;c  lines a b and c can be replaced with the line indicated below
     (  else    ( +  (* (funkyseq (- n 1)) (funkyseq (- n 3) ) )
                        (funkyseq (- n 2) )  ) ) ))

OR replce lines a b and c with:
  (  (< n 3)  (expt 5 n) )
    


Comments