[SOLVED] R Haskell prolog graph

$25

File Name: R_Haskell_prolog_graph.zip
File Size: 207.24 KB

5/5 - (1 vote)


Haskell and Prolog
Haskell Graphs
Overview
A simple way to represent a directed graph is via a list of edges. An edge can be thought of as a pair of nodes where the first element is the source node and the second element is the to node. For simplicity, we will assume that nodes are represented as integers. For example, in Haskell, we could define the following types
type Node = Integer
type Edge = (Integer, Integer)
type Graph = [Edge]
type Path = [Node]
These data types can be used to define graphs. For example, the following code defines two graphs: g and h.
g :: Graph
g = [(1,2), (1,3), (2,3), (2,4), (3,4)]
h :: Graph
h = [(1,2), (1,3), (2,1), (3,2), (4,4)]
Graph Functions
1.Write a function named nodes that takes a graph and returns a list of nodes (in ascending order). For example:nodes g => [1,2,3,4]
2.Write a function named adjacent that takes a node N and a graph G and returns a list of all nodes that are connected to N in G where N is the source. For example:adjacent 2 g => [3,4]adjacent 4 g => []adjacent 4 h => [4]
3.Write a function named detach that takes a node N and a graph G and returns the graph formed by removing node N from graph G. For example:detach 3 g => [(1,2), (2,4)]
4.Write a function named paths that accepts two nodes V1 and V2 and a graph G and returns a list of all possible cycle-free paths starting at V1 and ending in V2 in G. For example:paths 2 2 g => [[2]]paths 3 2 g => []paths 1 4 g => [[1,2,3,4], [1,2,4], [1,3,4]]
Prolog Rocks!
Overview
Write each the following Prolog rules.
Rules
5.grows(X): satisfied only when X is a list of numbers in increasing order. For example:grows([1,3,5,12]) succeedsgrows([3,5,2,3,7]) failsgrows([1,1,3,5]) fails
6.removed(L,M): satisfied only when list M contains all of the elements of list L but where all duplicates are removed.removed([1,2,3,1,2,3], M) ==> M = [1,2,3]removed([], M) ==> M = []
7.set_intersection(X,Y,Z). For this problem we represent a set as a list of elements. Of course, the list will not have duplicate entries. set_intersection(X,Y,Z) is satisfied only when Z is the set intersection of of two sets X and Y.
8.running(L,S): satisfied only when list S (a list of numbers) contains the running-sum of the numbers in list L. For example.running ([3,12,1,-4,9], R) ==> R = [3, 15, 16, 12, 21]running ([-3,-4,-5], R) ==> R = [-3,-7,-12]
9.list_merge(L1, L2, L3): satisfied only when L3 contains a sorted list of all elements contains in both sorted-list L1 and sorted-list L2. For example:list_merge([1,3,6,7],[2,4,5],M) ==> M = [1,2,3,4,5,6,7]
10.imports(LIB, IMPORTS): For this problem, we attempt to determine an ordered list of code-libraries, IMPORTS, that can be imported to satisfy all dependencies (direct or indirect) for code-library LIB. The dependsOn( LIB, LIBS ) is a fact that declares the immediate dependencies of code-library LIB. imports( LIB, IMPORTS ) is satisfied only when IMPORTS is a list that contains every code-library that code-library LIB depends upon (either directly or indirectly) such that all code-libraries on which any code-library in IMPORTS depends upon occur prior to that code-library in the list. Also, no code-library will occur more than once in the resulting import list. dependsOn( lib0, []).
11.dependsOn( lib1, []).
12.dependsOn( lib2, []).
13.dependsOn( lib3, []).
14.dependsOn( lib4, [lib3] ).
15.dependsOn( lib5, [lib0, lib2] ).
16.dependsOn( lib6, [lib5, lib3] ).
17.dependsOn( lib7, [lib1, lib3, lib4 ] ).
18.dependsOn( lib8, [lib3, lib5] ).
19.
20.imports( lib8, IMPORTS ) ==> IMPORTS =[lib0, lib2, lib3, lib5]
21.imports( lib7, IMPORTS ) ==> IMPORTS = [lib3, lib1, lib4]
22.
Requirements
Submit your code in GitLab under a folder named hw6. Within this folder you will have two files: graphs.hs and functors.pl corresponding to the Haskell and Prolog problem sets.
CS 421 : Programming Languages
Kenny Hunt

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] R Haskell prolog graph
$25