COMP0020: Functional Programming
Example Programs
COMP0020 Functional Programming Lecture 15a
Lists, Trees and Graphs
(link to implementation issues)
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 20 / 27
COMP0020: Functional Programming
Example Programs
Contents
Answer to student query :
Writing a function to traverse a cyclic graph
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 21 / 27
COMP0020: Functional Programming
Example Programs
Graphs : source code
multitree ú ::= Empty |Node ú [multitree ú] graph :: multitree char
graph = hd glist
glist :: [multitree char]
glist = [Node ‘AÕ [(glist ! 2), (glist ! 1)], Node ‘BÕ[(glist ! 3)], Node ‘CÕ [(glist ! 0),(glist ! 3)], Node ‘DÕ []]
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 22 / 27
COMP0020: Functional Programming
Example Programs
Graphs : representation after evaluating all links
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 23 / 27
COMP0020: Functional Programming
Example Programs
Graphs : source code
printgraph :: multitree char ≠ > [char]
printgraph Empty printgraph (Node x gs)
xpg :: [multitree char]
xpg [] y
xpg [Empty] y
xpg (Empty : gs) y
xpg((Nodexns):gs)y =”Node”++[x]++”seen”++s++(xpggsy),if(memberyx)
= ”Node ”++ [x]++ ”[”++ (xpg ns (x : y))++ z ++ (xpg gs (x : y)), otherwise where s = ””, if gs = []
= ”, ”, otherwise z =”]”++s
= []
= “Node ” ++ [x] ++“[“ ++ (xpg gs [x]) ++”]”
≠ > [char]≠ > [char]
= []
= ”Empty”
= ”Empty, ” ++ (xpg gs y)
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 24 / 27
COMP0020: Functional Programming
Example Programs
Summary
Summary
Answer to student query :
Writing a function to traverse a cyclic graph
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 25 / 27
COMP0020: Functional Programming
Example Programs
Summary
END OF LECTURE
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 26 / 27
COMP0020: Functional Programming
Example Programs
Summary
END OF ALL LECTURES
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2018-2019 27 / 27