Values, functions and data
Chapter 1’s program greets the world. From here on, you build something
more useful: a todo list. Create src/hello/todo.hf, and add each
definition in this chapter to it, in order, after its name:
module hello.todoData types
data Status = Open | DoneA data type lists its constructors. A Status is either Open or
Done.
Records
type Todo = { title : String, status : Status }
todo : String -> Todotodo title = { title = title, status = Open }
finish : Todo -> Todofinish t = { t | status = Done }A record type names its fields. t.title reads a field, and
{ t | status = Done } is t with one field replaced.
Pattern matching
pending : Status -> Boolpending Open = Truepending Done = False
isOpen : Todo -> BoolisOpen t = pending t.statusA function can have one equation per pattern.
As text
instance toStringStatus : Show StatustoStringStatus = { show = mark } where mark : Status -> String mark Open = "[ ]" mark Done = "[x]"
instance toStringTodo : Show TodotoStringTodo = { show = \t -> show t.status ^ " " ^ t.title }toStringStatus comes before toStringTodo because a definition only
sees the ones above it.
Lists
title : Todo -> Stringtitle t = t.title
openTitles : List Todo -> List StringopenTitles ts = List.map title (List.keepIf isOpen ts)List.keepIf keeps the todos that are open, and List.map takes the
title of each.