[an error occurred while processing this directive]

cs150: Notes 8

Assignments Due

Tracing

To enable tracing, evaluate (require (lib "trace.ss")). (You can put this in your definitions file, like it is in poker.scm.) Then,
(trace procedure)
will enable tracing of the procedure. The interpreter will print out a message corresponding to the entrance (showing the parameters) and exit (showing the resulting value) of the traced procedure. This is a useful tool for understanding what is going on when your programs are evaluated.

List Procedures

(define (list? p)
  (if (null? p) 
      #t
      (if (pair? p)
          (list? (cdr p))
          #f)))
(define (sumlist p)
  (if (null? p)
      0
      (+ (car p) (sumlist (cdr p)))))
(define (map f p)
  (if (null? p)
      null
      (cons (f (car p)) 
               (map f (cdr p)))))

Here's an example use of map in the choose-n procedure from PS2:
(define (choose-n n lst)
  ;; operands: a number n and a list (of at least n elements)
  ;; result: evaluates to a list of all possible ways 
  ;;     of choosing n elements from lst
  (if (= n 0) 
      (list null) 
      (if (= (length lst) n) 
          (list lst) 
          (append (choose-n n (cdr lst)) 
                  (map (lambda (clst) (cons (car lst) clst)) 
                       (choose-n (- n 1) (cdr lst)))))))
What is the map application expression doing?






(define (list-cruncher base proc combiner lst)
  (if (null? lst)
      base
      (combiner (proc (car lst))
                     (list-cruncher base proc combiner 
                                         (cdr lst)))))


(define (sumlist p)

  (list-cruncher _____ ___________________  _____ p))

(define (map f p)

  (list-cruncher _______  ______   ______ p))

(define (length p)

  (list-cruncher ______  _____________ + p))
Why is it impossible to correctly define list? using just list-cruncher?





Define find-closest-number, a procedure that takes two parameters, a goal and a list of numbers, and produces the number in the list numbers list that is closest to goal.





[an error occurred while processing this directive]