Skip to content

Commit

Permalink
solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
gaecoli authored Nov 8, 2020
1 parent 8806ef5 commit a9a2399
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Exercise_246.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#lang racket

(check-expect (extract < (cons 6 (cons 4 '())) 5) (list 4))
(check-expect (extract > (cons 6 (cons 4 '())) 5) (list 6))

(define (extract R l t)
(cond
[(empty? l) '()]
[else (cond
[(R (first l) t)
(cons (first l)
(extract R (rest l) t))]
[else
(extract R (rest l) t)])]))


(extract < (cons 6 (cons 4 '())) 5)

(extract < (cons 4 '()) 5)
16 changes: 16 additions & 0 deletions Exercise_247.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#lang racket

(check-expect (extract < (cons 6 (cons 4 '())) 5) (list 4))
(check-expect (extract > (cons 6 (cons 4 '())) 5) (list 6))

(define (extract R l t)
(cond
[(empty? l) '()]
[else (cond
[(R (first l) t)
(cons (first l)
(extract R (rest l) t))]
[else
(extract R (rest l) t)])]))

(extract < (cons 8 (cons 4 '())) 5)
8 changes: 8 additions & 0 deletions Exercise_248.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#lang racket
(define (squared>? x c)
(> (* x x) c))


(squared>? 3 10)

(squared>? 4 10)
9 changes: 9 additions & 0 deletions Exercise_249.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#lang racket

(define (f x) x)

(cons f '())

(f f)

(cons f (cons 10 (cons (f 10) '())))
40 changes: 40 additions & 0 deletions Exercise_250.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#lang racket

(check-within (tabulate sin 5) (tab-sin 5) 0.000001)
(check-within (tabulate sqrt 5) (tab-sqrt 5) 0.000001)

(define (tabulate g n)
(cond
[(= n 0) (list (g 0))]
[else
(cons
(g n)
(tabulate g (sub1 n)))]))


(check-expect (tab-sqr 3) (list 9 4 1 0))

(define (tab-sqr n)
(tabulate sqr n))

(check-within (tab-tan 3) (list (tan 3) (tan 2) (tan 1) (tan 0)) 0.000001)

(define (tab-tan n)
(tabulate tan n))


(define (tab-sin n)
(cond
[(= n 0) (list (sin 0))]
[else
(cons
(sin n)
(tab-sin (sub1 n)))]))

(define (tab-sqrt n)
(cond
[(= n 0) (list (sqrt 0))]
[else
(cons
(sqrt n)
(tab-sqrt (sub1 n)))]))
23 changes: 23 additions & 0 deletions Exercise_251.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#lang racket

(check-expect (fold1 '(1 2 3 4 5) 0 +) (sum '(1 2 3 4 5)))
(check-expect (fold1 '(1 2 3 4 5) 1 *) (product '(1 2 3 4 5)))

(define (fold1 l i g)
(cond
[(empty? l) i]
[else
(g (first l)
(fold1 (rest l) i g))]))


(check-expect (fold1-sum '(1 2 3 4 5)) (+ 1 2 3 4 5))

(define (fold1-sum l)
(fold1 l 0 +))


(check-expect (fold1-product '(1 2 3 4 5)) (* 1 2 3 4 5))

(define (fold1-product l)
(fold1 l 1 *))
32 changes: 32 additions & 0 deletions Exercise_252.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#lang racket

(define LO-N1 (list 1 2 3 4 5 6))
(define LO-POSN1 (list (make-posn 20 30) (make-posn 40 70) (make-posn 50 50)))

(define emt
(empty-scene 100 100))
(define dot
(circle 3 "solid" "red"))


(check-expect (fold2 LO-N1 1 *) (product LO-N1))
(check-expect (fold2 LO-POSN1 emt place-dot) (image* LO-POSN1))

(define (fold2 l i g)
(cond
[(empty? l) i]
[else
(g (first l)
(fold2 (rest l) i g))]))


(check-expect (fold2-product LO-N1) (product LO-N1))

(define (fold2-product l)
(fold2 l 1 *))


(check-expect (fold2-image* LO-POSN1) (image* LO-POSN1))

(define (fold2-image* l)
(fold2 l emt place-dot))
11 changes: 11 additions & 0 deletions Exercise_253.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#lang racket

(check-expect (number? 5) #true)

(check-expect (equal? #true "abde") #false)

(check-expect (+ 1 2 3) 6)

(check-expect (list 5) (list 5))

(check-expect (list? '(1 2 3 4)) #true)
4 changes: 4 additions & 0 deletions Exercise_256.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#lang racket

(check-expect (argmax second (list (list "a" 4) (list "b" 2) (list "c" 9) (list "d" 5)))
(list "c" 9))

0 comments on commit a9a2399

Please sign in to comment.