2015-10-08

posted Oct 8, 2015, 4:51 AM by Samuel Konstantinovich   [ updated Oct 8, 2015, 6:30 AM ]
Goal: Lists 

also HW / Exam related info.



We write a list like this:

'(4 2 "hello" 5.4)

The apostrophe ' means do not evaluate the next thing.

We can bind a variable to a list like any other value: 

(define L '(1 2 3) )

A list has two parts, the 1st element, and everything else.

car
cdr
null?




Prior Solutions:

Versions of military time:

(define (militaryTime h ampm)
  (cond
    ( (and (= h 12) (= ampm 0))  0 )
    ( (and (= h 12) (= ampm 1))  12)
    ( (= ampm 0)  h)
    ( else       (+ h 12))))

;check if ampm is 0 or 1, use remainder
(define (militaryTime h ampm)
  (if (= ampm 0)
      (remainder h 12)
      (+ 12 (remainder h 12)) ))

;check if 12 or not, use the fact that ampm is 0 or 1
(define (militaryTime h ampm)
  (if (not (= h 12))
      (+ h (* 12 ampm))
      (* ampm 12)))

;use the remainder AND also use the fact that ampm is 0 or 1
(define (militaryTime h ampm)
      (+ (remainder h 12) (* 12 ampm)))
  

Version of leapyear:

(define (leapyear year)
   (or
       (and 
         (not (= 0(remainder year 100))) 
         (= 0 (remainder year 4)))
       (= 0(remainder year 400))))
  

(leapyear 1900)
(leapyear 2000)
(leapyear 2001)
(leapyear 2004)
Comments