This is a course page of David Casperson |
|
All homework for CPSC 370 Fall 2016 now has an assigned due date below.
x
. Handle the
following rules
Here is a specification by example:
parse_comments("{cat}",X).
succceeds with X=[comment(["cat"])]
.
parse_comments("{cat}{dog}",X).
succceeds with X=[comment(["cat"]), comment(["dog"])]
.
parse_comments("a{cat}house{dog}boat",X).
succceeds with
X=["a", comment(["cat"]), "house", comment(["dog"]),
"boat"]
.
parse_comments("}{cat}",X).
fails.
Only slightly harder, also handle nested comments.
parse_comments("{{cat}}",X).
succceeds with X=[ comment([
comment(["cat"]) ]) ]
.
parse_comments("{{a}b}c",X).
succceeds with X=[ comment([
comment(["a"]), "b"]), "c" ]
.
Here is a slightly tangled set of rules
comment(
…)
where the dots represent a scan.
Here is an implementation strategy. Write
parse_comments(String,Rep) :- string_codes(String,Codes), phrase(aScan(Rep),Codes).Here
string_codes/2
and phrase/2
are
standard predicates. You need to write a DCG clause
for aScan
.
data LeafTree a = Leaf a | Branch (LeafTree a) (LeafTree a)write a function with signature
removeIf :: (a -> Bool) -> LeafTree a -> Maybe (LeafTree a)that removes all of the elements from a given LeafTree that satisfy the given predicate. The result should be Nothing only when all of the elements have been removed.
Hint: write a helper function
branchMaybe :: Maybe (LeafTree a) -> Maybe (LeafTree a) -> Maybe (LeafTree a)
to join together two (Maybe) trees.
data Tree a = Empty | Node a (Tree a) (Tree a)write a function with signature
removeIf :: (a -> Bool) -> Tree a -> Tree athat removes all of the elements from a given Tree that satisfy the given predicate.
Hint: write helper functions
join :: (Tree a) -> (Tree a) -> (Tree a) nodeMaybe :: (Maybe a) -> (Tree a) -> (Tree a) -> (Tree a)
to join together two trees or two trees and a value.
1
.
(require rackunit)
.
uncurry
function
in Haskell so that if we have the
definitions
ff (a,b) = 2*a - 3 - b
f a b = 2*a - 3 - b
g = uncurry f
then the functions ff
and g
work
identically.
curry
function
in Racket.
Read Chapter 2 of Learn You a Haskell for Great Good. Send me an e-mail message explaining what you understand to be the differences between lists and tuples.
How many of these are fair and interesting?
1
.
(require rackunit)
.
uncurry
function
in Haskell so that if we have the
definitions
ff (a,b) = 2*a - 3 - b f a b = 2*a - 3 - b g = uncurry ffthen the functions
f
and g
work
identically.
curry
function
in Racket.
fall-2024