Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is a Haskell question. Please provide the definitions for the functions in Haskell. A simple way to represent a directed graph is through a
This is a Haskell question. Please provide the definitions for the functions in Haskell.
A simple way to represent a directed graph is through a list of edges. An edge is given by a pair of nodes. For simplicity, nodes are represented by integers. typeNodetypeEdgetypeGraphtypePath=Int=(Node,Node)=[Edge]=[Node] (We ignore the fact that this representation cannot distinguish between isolated nodes with and without loops; see, for example, the loop/edge (4,4) in the graph h that represents an isolated node.) Consider, for example, the following directed graphs. These two graphs are represented as follows. g::Graphg=[(1,2),(1,3),(2,3),(2,4),(3,4)]h::Graphh=[(1,2),(1,3),(2,1),(3,2),(4,4)] Note: In some of your function definitions you might want to use the function norm (defined in the file HW1types. hs) to remove duplicates from a list and sort it. (a) Define the function nodes :: Graph [Node] that computes the list of nodes contained in a given graph. For example, nodes g=[1,2,3,4]. (b) Define the function suc :: Node Graph [Node] that computes the list of successors for a node in a given graph. For example, suc 2g=[3,4], suc 4g=[], and suc 4h=[4]. (c) Define the function detach :: Node Graph Graph that removes a node together with all of its incident edges from a graph. For example, detach 3g=[(1,2),(2,4)] and detach 2h=[(1,3),(4,4)]. (d) Define the function cyc :: Int Graph that creates a cycle of any given number. For example, cyc 4= [(1,2),(2,3),(3,4),(4,1)]. Note: All functions can be succinctly implemented with list comprehensions
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started