(define (sumsq l)
(if (null? l)
0
(+ (expt (car l) 2)
(sumsq (cdr l)) ) ) )
(define (countsymbols l)
(if (null? l)
0
(+ (if (symbol? (car l)) 1 0)
(countsymbols (cdr l)) ) ) )
(define (avgnums l)
(let ((sum 0) (count 0))
(dolist (item l)
(if (number? item)
(begin (set! sum (+ sum item))
(set! count (1+ count))) ) )
(/ sum count) ))
The following solution uses a tail-recursive auxiliary function:
(define (avgnums l) (avgnumsb l 0 0))
(define (avgnumsb l sum count)
(if (null? l)
(/ sum count)
(if (number? (car l))
(avgnumsb (cdr l) (+ sum (car l)) (1+ count))
(avgnumsb (cdr l) sum count))))
(int-count '(big dog) '(a big dog has a big appetite)) = 3
(define (int-count int l) (int-countb int l 0))
(define (int-countb int l answer)
(if (pair? l)
(int-countb int (cdr l) (+ answer
(if (member (car l) int)
1
0)))
answer))
(define (int-count int l)
(let ((count 0))
(dolist (x l)
(if (member x int)
(set! count (+ count 1))) )
count))
(divideby '(3 4 6 17 21 0) 3) = (3 6 21 0)
(define (divideby lst n)
(if (pair? lst)
(if (= (modulo (car lst) n) 0)
(cons (car lst) (divideby (cdr lst) n))
(divideby (cdr lst) n))
'() ) )
(addpoints '(smith (exam 33) (lab 40) (extra 10))) = 83
(define (addpoints lst) (addpointsb (cdr lst)))
(define (addpointsb lst)
(if (pair? lst)
(+ (cadr (car lst))
(addpointsb (cdr lst)))
0))
(define (addpoints lst)
(let ((total 0))
(dolist (sublist (cdr lst))
(set! total (+ total (cadr sublist))) )
total))
(profit '((garden 15000 10000) (kitchen 2000 1000)))
= 6000
It is helpful to define small functions that document what is being done:
(define sales cadr)
(define costs caddr)
(define (deptprofit dept) (- (sales dept) (costs dept)))
(define (profit lst)
(if (pair? lst)
(+ (deptprofit (car lst))
(profit (cdr lst)))
0))
Without the documenting functions, the result is correct but less clear:
(define (profit lst)
(if (pair? lst)
(+ (- (cadr (car lst)) (caddr (car lst)))
(profit (cdr lst)))
0))
(invert '((john 3271234) (mary 4449876)) )
= ((3271234 john) (4449876 mary))
(define (invert lst)
(if (pair? lst)
(cons (reverse (car lst))
(invert (cdr lst)))
'()))
(points '(pawn rook pawn)
'((queen 10) (rook 5) (pawn 1))) => 7
(define (points lst pts)
(if (pair? lst)
(+ (cadr (assoc (car lst) pts))
(points (cdr lst) pts))
0))
1 3 5 7 9
x x x x x
sin(x) = -- - -- + -- - -- + -- - ...
1! 3! 5! 7! 9!
Write a function (sine x) to compute sine using this series.
You may not use the functions expt or factorial; instead,
write a tail-recursive auxiliary function to compute each
term of the series from the previous term and add it to the
sum. Stop after the 21st power of x. Hint: write down how
each term of the series differs from the previous term.
(define (sine x) (sineb x 3 (* x x) x))
(define (sineb term n xsq sum)
(if (> n 21)
sum
(let ((newterm (- (/ (* term xsq) (* (1- n) n)))))
(sineb newterm (+ n 2) xsq (+ sum newterm)) ) ) )
(select 'cs 2000
'((john cs 1980) (mary math 1982) (jane cs 1982)))
=> ((john 20) (jane 18))
(define (select major year lst)
(if (pair? lst)
(if (eq? (cadr (car lst)) major)
(cons (list (car (car lst))
(- year (caddr (car lst))))
(select major year (cdr lst)))
(select major year (cdr lst)))
'() ) )