-- "type" introduces a new name for an existing type -- like a typedef/struct in C/C++ -- type and constructor names must begin with a capital letter type String = [Char] type Id = Int type Name = String type Age = Int type Gender = Char type Rate = Float --type Professor = (Id, Name, Gender, Age, Float) type Professor = (Id, Name, Gender, Age, Rate) {-- struct { int a; float b; } --} tam :: Professor tam = (1, "Tam Nguyen", 'f', 46, 45.56) shen :: Professor shen = (2, "Ju Shen", 'm', 64, 7.25) yao :: Professor yao = (5, "Zhongmei Yao", 'F', 31, 8.25) phung :: Professor phung = (1, "Phu Phung", 'M', 32, 3.25) saverio :: Professor saverio = (2, "Saverio Perugini", 'M', 20, 1.23) gowda :: Professor gowda = (3, "Raghava Gowda", 'M', 59, 13.25) zargham :: Professor zargham = (4, "Mehdi Zargham", 'M', 55, 33.25) type Department = [Professor] udcps :: Department udcps = [tam,shen,phung,yao,saverio,gowda,zargham] type Point = (Float, Float) type Rectangle = (Point, Point, Point, Point) -- can be parameterized like a template in C++ type Mapping domain_type range_type = [(domain_type , range_type)] floor1 :: Mapping Float Int floor1 = [(2.1,2),(2.2,2),(2.9,2)] professor_mapping :: Mapping Int [Char] professor_mapping = [(1, "Zhongmei Yao"), (2, "Saverio Perugini")] lookup :: Mapping Professor Id lookup = [(yao,1), (saverio,2)] -- recursive types not permitted [PIH] p. 99 -- type Tree = (Int, [Tree]))
-- a variant record or a union of structs -- comparable to define-datatype datatype Bool = True | False; datatype Colors = Red | Green | Blue | Orange | Yellow; -- can be parameterized (like a template in C++) datatype 'a Student = New | Id of 'a; -- can be recursive datatype Natural = Zero | Succ of Natural; datatype IntTree = Leaf of int | Node of IntTree * int * IntTree; -- can be parameterized and recursive datatype 'a List = Nil | Cons of 'a * ('a List);
(* a variant record or a union of structs like the define-datatype construct from [EOPL2] or Haskell's data *) datatype Bool = True | False; datatype Colors = Red | Green | Blue | Orange | Yellow; (* union { int a; float b; } *) datatype daysofourlives = Sun | Mon | Tue | Wed | Thu | Fri | Sat; (* fun onholiday(Sun) = true | onholiday(day) (day = Sun) orelse (day = Sat); *) fun onholiday(day) = (day = Sun) orelse (day = Sat); val ans = onholiday(Mon); val ans2 = onholiday(Sat); (* can be parameterized like a template in C++ *) datatype 'a Student = New | Id of 'a; (* can be recursive *) datatype Natural = Zero | Succ of Natural; val four = (Succ (Succ (Succ (Succ Zero)))); datatype Bintreeofints = EmptyI | NodeI of Bintreeofints * int * Bintreeofints; (* ourbintreeofints : Bintreeofints; *) val ourbintreeofints = NodeI ( NodeI ( NodeI (EmptyI, 1, EmptyI), 7, NodeI (EmptyI, 2, EmptyI)), 6, NodeI ( NodeI (EmptyI, 3, EmptyI), 8, NodeI ( NodeI (EmptyI, 5, EmptyI), 4, NodeI (EmptyI, 10, EmptyI)))); (* if inorder returns a sorted list, then its argument is a binary search tree *) fun inorderI EmptyI = nil | inorderI (NodeI (left, i, right)) = (inorderI left) @ [i] @ (inorderI right); fun preorderI EmptyI = nil | preorderI (NodeI (left, i, right)) = [i] @ (preorderI left) @ (preorderI right); fun postorderI EmptyI = nil | postorderI (NodeI (left, i, right)) = (postorderI left) @ (postorderI right) @ [i]; val ans3 = inorderI ourbintreeofints; val ans4 = preorderI ourbintreeofints; val ans5 = postorderI ourbintreeofints; (* like typedef in C/C++ *) type id = int; type name = string; type age = int; type gender = char; type rate = real; type professor = (id * name * gender * age * rate); (* type professor = (id * name * gender * age * real); *) (* struct { int a; float b; } *) val tam = (1, "Tam Nguyen", #"M", 46, 45.56) : professor; val shen = (2, "Ju Shen", #"M", 64, 7.25) : professor; val yao = (5, "Zhongmei Yao", #"M", 31, 8.25) : professor; val phu = (1, "Phu Phung", #"M", 32, 3.25) : professor; val saverio = (2, "Saverio Perugini", #"M", 20, 1.23) : professor; val gowda = (3, "Raghava Gowda", #"M", 59, 13.25) : professor; val zargham = (4, "Mehdi Zargham", #"M", 55, 33.25) : professor; type department = professor list; type department = professor list; val udcps = [tam,shen,phu,saverio,gowda,zargham,yao]; val sun = (8, "Yvonne Sun", #"F", 29, 4.5); val mark = (9, "Mark Nielsen", #"M", 51, 5.7); val udbio = [sun,mark]; val joe = (10, "Joe Mashburn", #"M", 52, 10.1); val art = (11, "Art Busch", #"M", 33, 5.66); val atif = (12, "Atif Abueida", #"M", 42, 6.67); val driskell = (13, "Shannon Driskell", #"F", 35, 7.753); val udmat = [joe,art,atif,driskell]; type univ = department list; val ud = [udcps,udbio,udmat]; (* can be parameterized and recursive *) datatype 'a List = Nil | Kons of 'a * ('a List); val cpslistofprofs : professor List = Kons (phu, Kons (saverio, Kons (gowda, Kons (yao, Kons (zargham, Nil))))) (* parameterized datatype *) datatype 'a Bintree = Empty | Node of 'a Bintree * 'a * 'a Bintree; val ourbintreeofints2 = Node ( Node ( Node (Empty, 1, Empty), 7, Node (Empty, 2, Empty)), 6, Node ( Node (Empty, 3, Empty), 8, Node ( Node (Empty, 5, Empty), 4, Node (Empty, 10, Empty)))); val ourbintreeofstrs : string Bintree = Node ( Node ( Node (Empty, "one", Empty), "seven", Node (Empty, "two", Empty)), "six", Node ( Node (Empty, "three", Empty), "eight", Node ( Node (Empty, "five", Empty), "four", Node (Empty, "ten", Empty)))); val ourbintreeofstrs2 : string Bintree = Node ( Node ( Node(Empty, "the", Empty), "type", Node(Empty, "is", Empty)), "cat", Node ( Node (Empty, "called", Empty), "bintree", Node (Empty, "and", Empty))); val ourbintreeofEmployees = Node ( Node ( Node (Empty, saverio, Empty), phu, Node (Empty, gowda, Empty)), yao, Node ( Node (Empty, phu, Empty), zargham, Node ( Node (Empty, yao, Empty), gowda, Node (Empty, saverio, Empty)))); val ourbintreeofDepts = Node ( Node (Empty, udcps, Empty), udmat, Node (Empty, udbio, Empty)); fun inorder Empty = nil | inorder (Node( left, i, right)) = (inorder left) @ [i] @ (inorder right); fun preorder Empty = nil | preorder (Node (left, i, right)) = [i] @ (preorder left) @ (preorder right); fun postorder Empty = nil | postorder (Node (left, i, right)) = (postorder left) @ (postorder right) @ [i]; val ans6 = inorder ourbintreeofstrs; val ans7 = preorder ourbintreeofstrs; val ans8 = postorder ourbintreeofstrs; val ans9 = inorder ourbintreeofstrs2; val ans10 = preorder ourbintreeofstrs2; val ans11 = postorder ourbintreeofstrs2; val ans12 = inorder ourbintreeofEmployees; val ans13 = preorder ourbintreeofEmployees; val ans14 = postorder ourbintreeofEmployees;
concept | ML | Haskell |
---|---|---|
lists | homogeneous | homogeneous |
cons | :: | : |
append | @ | ++ |
renaming parameters | lst as (x::xs) | lst@(x:xs) |
functional redefinition | permitted | not permitted |
dynamic dispatch | | | |
parameter passing | call-by-value
strict applicative-order evaluation |
call-by-need
non-strict normal-order evaluation |
functional composition | o | . |
infix → prefix | (op +) | (+) |
curried form | omit parentheses | omit parentheses |
type declaration | : | :: |
datatype definition | datatype | data |
type variables | prefaced with '
written before the datatype name |
not prefaced with '
written after the datatype name |
function type | optional, but if used,
embedded within function definition |
optional, but if used, precedes the function definition |
type checking | Hindley-Milner | Hindley-Milner |
function overloading | not supported | supported through qualified types and type classes |
(extend-environment symbols values ... (extend-environment symbols values (empty-environment)))and define them using EBNF (a concrete syntax)
<environment> ::= ??? <environment> ::= ???
[COPL9] | R.W. Sebesta. Concepts of Programming Languages. Addison-Wesley, Ninth edition, 2010. |
[EOPL2] | D.P. Friedman, M. Wand, and C.T. Haynes. Essentials of Programming Languages. MIT Press, Cambridge, MA, Second edition, 2001. |
[EMLP] | J.D. Ullman. Elements of ML Programming. Prentice Hall, Upper Saddle River, NJ, Second edition, 1997. |
[PIH] | G. Hutton. Programming in Haskell. Cambridge University Press, Cambridge, 2007. |
[PLPP] | K.C. Louden. Programming Languages: Principles and Practice. Brooks/Cole, Pacific Grove, CA, Second edition, 2002. |
[WFPM] | J. Hughes. Why Functional Programming Matters. The Computer Journal, 32(2), 98-107, 1989. |