;;; ;;; Part I. ;;; ; 1.) (define x 10) (define y 44) (define foo '(1 2 3)) (define bar '(a b c)) x => 10 (cons x foo) => (10 1 2 3) (cons y bar) => (44 a b c) (list x y) => (10 44) (list x foo) => (10 (1 2 3)) '(x y) => (x y) (car bar) => a (cdr bar) => (b c) (+ x y) => 54 ; 2.) (if #f 5 10) => 10 (if (or #t #f) 5 10) => 5 (if #t (if #f 2 3) 10 ) => 3 (define x 3) (cond ((= x 1) 44) ((= x 2) (+ 5 50)) ((= x 3) (* 6 11)) (else (- 80 3))) => 66 (let ((x 5) (y 10)) (+ x y)) => 15 ; 3.) (define (char-hex-letter? c) (or (char<=? #\a c #\f) (char<=? #\A c #\F) )) (define (char-hex? c) (if (char? c) (or (char-hex-letter? c) (char<=? #\0 c #\9)) (error "passed char-hex? a non-character"))) ;;; ;;; Part II. ;;; ; 1.) ; helper function (define (char-hex-letter->number c) (if (char<=? #\a c #\f) (+ 10 (- (char->integer c) (char->integer #\a))) (+ 10 (- (char->integer c) (char->integer #\A))))) (define (char-hex->number c) (if (char-hex-letter? c) (char-hex-letter->number c) (if (char-hex? c) ; we know it's a number now (- (char->integer c) (char->integer #\0)) (error "passed char-hex->number something not in [0-9a-fA-F]")))) ; 2.) ; helper function (define (string-hex->number-iter s counter size total) (if (>= counter size) total (string-hex->number-iter s (+ counter 1) size (+ (* 16 total) (char-hex->number (string-ref s counter)))))) (define (string-hex->number s) (if (string? s) (string-hex->number-iter s 0 (string-length s) 0) (error "passed string-hex->number a non-string"))) ; 3.) (define (positive-only x) (cond ((null? x) '()) ((> (car x) 0) (cons (car x) (positive-only (cdr x)))) (else (positive-only (cdr x))) ) )