Download presentation

Presentation is loading. Please wait.

Published byChristine Boyson Modified over 2 years ago

1
Natural Numbers or, "All you ever need is compounds, variants, and recursion".

2
Today's Lecture We've seen types like: [any] -> natural[num] -> num [num] -> [num][X], [X] -> [X] Let's check out this guy: flatten : [[X]] -> [X] And look at "lists without baggage" Close look at "recursion over what?"

3
Lists of different colors We've seen [any] : the kitchen sink [num] : sequences, n-D points, graph … distance-from-0 = (sqrt (sum (map-square l))) What about [car], [course-info], [posn] Could [[course-info]] be useful?

4
Flatten ; flatten : [[X]] -> [X] ; Example : (flatten [ [1,2,3], [4], [5,6] ]) ; [1,2,3,4,5,6] (define (flatten x) (cond [(empty? x) empty] [else (append (first x) (flatten (rest x)))])))

5
Make your own naturals ; A Natural is ; - 'Zero ; - (make-next nat) where nat is a Natural (define-struct next (nat)) ; Template: ; (define (f m) ; (cond [(symbol? m)...] ; [else (... (f (next-nat m)))]))

6
Importing them ; natIt : number -> Natural ; purpose : convert numbers into Naturals ; example : 0 -> 'Zero, 1 -> (make-next ‘zero), ; 2 -> (make-next (make-next 1))... (define (natIt n) (cond [(= n 0) 'Zero] [else (make-next (natIt (- n 1)))]))

7
Exporting them ; numIt : Natural -> number ; purpose : convert a Natural to a number ; example : 0 <- 'Zero, 1 <- (make-next 1), ; 2 <- (make-next (make-next 1))... (define (numIt n) (cond [(symbol? n) 0] [else (+ 1 (numIt (next-nat n)))]))

8
Addition ; add : Natural, Natural -> Natural (define (add n m) (cond [(symbol? n) m] [else (make-next (add (next-nat n) m))])) Induction on n

9
Multiplication ; mult : Natural, Natural -> Natural (define (mult n m) (cond [(symbol? n) 'Zero] [else (add m (mult (next-nat n) m))])) Induction on n

10
Exponentiation ; exp : Natural, Natural -> Natural (define (expo n m) (cond [(symbol? m) (make-next 'Zero)] [else (mult n (expo n (next-nat m)))])) Induction on m

11
Greater or equal to ; geq : Natural, Natural -> Natural (define (geq n m) ; Induction on m (cond [(symbol? m) true] [else (cond [(symbol? n) false] [else (geq (next-nat n) (next-nat m))])]))

12
Subtraction ; minus : Natural, Natural -> Natural (define (minus n m) ; Induction on m & n (cond [(symbol? m) n] [(symbol? n) 'Zero] [else (minus (next-nat n) (next-nat m))]))

13
Division ; divi : Natural, Natural -> Natural (define (divi n m) ; Strong Induction on n (cond [(symbol? (next-nat m)) n] [(not (geq n m)) 'Zero] [else (make-next (divi (minus n m) m))]))

Similar presentations

OK

Scheme & Functional Programming. (+ 23 41) >> 64 (- 1000 334) >> 666 (* 25 4 12) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.

Scheme & Functional Programming. (+ 23 41) >> 64 (- 1000 334) >> 666 (* 25 4 12) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on viruses and bacteria video Ppt on bank lending rates Ppt on book of revelation Ppt on different aspects of environment and society Ppt on porter's five forces pdf Ppt on channels of distribution chart Ppt on self development charter Ppt on disk formatting utilities Ppt on bluetooth security Ppt on loops in c language