;; ANEL
is one of: ;; ---(cons Number empty)
;; ---(cons Number NEL)
;;NEL → Number
;; find the largest number inl
;; examples:(list 1 2 3)
produces3
(define (largest l) (cond [(empty? (rest l)) (first l)] [else (cond [(> (first l) (largest (rest l))) (first l)] [else (largest (rest l))])])) ;; test: (= (largest (list 1 2 3)) 3) ;; run program run (largest (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17)) ; produces 17 (largest (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23))
Use local
definitions instead:
;;NEL → Number
;; find the largest number inl
;; examples:(list 1 2 3)
produces3
(define (largest l) (cond [(empty? (rest l)) (first l)] [else (local ((define max-in-rest (largest (rest l)))) (cond [(> (first l) max-in-rest) (first l)] [else max-in-rest])])))
;;N → Matrix
;; create a 1 diagonal in an n x n square (define (diagonal n) (local (;;N → Matrix
;; produce i rows of width n with 1’s in the diagonal positions ;; example:(aux n)
produces (list (list 0 0 ... 1)) (define (aux i) (cond [(zero? i) empty] [else (cons (row n i) (aux (sub1 i)))])) ;;N → NumberList
;; produce one row of widthn
with a 1 in position i ;; example:(row 3 1)
produces(list 1 0 0)
(define (row n i) (cond [(zero? n) empty] [else (cond [(= i n) (cons 1 (row (sub1 n) i))] [else (cons 0 (row (sub1 n) i))])]))) (aux n))) ;; tests: (equal? (diagonal 3) (list (list 1 0 0) (list 0 1 0) (list 0 0 1))) (equal? (diagonal 1) (list (list 1))) (equal? (diagonal 0) empty)
|