This is a course web page of David Casperson |
|
All questions are now due.
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.
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 (LeafTree a) (LeafTree 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.
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
.
fall-2024