Recursive Data Types
Total Page:16
File Type:pdf, Size:1020Kb
RECURSIVE DATA TYPES DR. ALEX GERDES UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY Today • Recursive data types • Case expressions UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY Types are sets of values • In Haskell data Bool = False | True data Suit = Spades | Hearts | Diamonds | Clubs data Rank = Numeric Int | Jack | Queen | King | Ace data Card = Card Rank Suit • In math: – Bool = {False, True} – Suit = {Spades, Hearts, Diamonds, Clubs} – Rank = {Numeric n | n ∈ Int} ∪ {Jack, Queen, King, Ace} – Card = {Card r s | r ∈ Rank, s ∈ Suit} UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY Circular definitions, do they make sense? fac 0 = 1 fac n = n * fac(n-1) • Recursive functions: make sense, if you follow some basic rules f x = f x • Circular definitions of numbers? – They can make sense when seen as as equations in math, but as programs x,y :: Int they usually make no sense… x = x y = 2 * y • Circular definitions of lists? – They can make sense – This works in Haskell because Haskell ha,haha :: String uses lazy evaluation ha = "ha"++ha haha = take 10 ha UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY The meaning of circular definition of types • What set of values does this correspond to? – Nat = {Zero} ∪ {Succ n | n ∈ Nat} -- Peano's natural numbers • But this is still a circular definition… data Nat = Zero | Succ Nat • We can instead view the alternatives in data type definitions as rules that together generate the intended set: – Rule 1: Zero ∈ Nat – Rule 2: if n ∈ Nat then Succ n ∈ Nat • Recursive data types are also called inductive data types UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY Inductively defined sets • By using induction we can define sets that contain infinitely many elements – Finite sets: enumerate all the elements – Infinite sets: give a finite set of rules that describe how to generate the elements • Recursive data type definitions create data types with infinitely many values – The values can also be arbitrarily large § For example, there is no limit on how long lists can be – We can work with data structures whose size is not known in advance UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY LIVE CODING UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY Recursive data types • The values in these types are all linear sequences of things. data Hand = Empty | Add Card Hand data List a = Nil | Cons a (List a) data Nat = Zero | Succ Nat -- Example values of these types: hand = Add c1 (Add c2 (Add c3 Empty) list = Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))) number = Succ (Succ (Succ Zero)) UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY Branching data structures • How would we create a data type for flowcharts (action diagrams)? • More examples of flowcharts UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY LIVE CODING UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY A simpler tree-shaped data type • Binary trees are trees where nodes have two subtrees data BTree = Leaf | Node BTree Int Btree UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY KAHOOT UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY UNIVERSITY OF GOTHENBURG | CHALMERS UNIVERSITY OF TECHNOLOGY.