This is a course page of David Casperson |
|
All Homework is now due.
undefined
error
flip
where flip f x y = f y x
const
where const x y = x
const False
id
where id x = x
curry
where curry f x y = f(x,y)
>uncurry
where uncurry f (x,y) = f x
y
(Thiw is also question 16.)A combinator is a function that depends on nothing other than its arguments. There are a number of combinators built in to Haskell, as illustrated by the last question.
Another thoeretically important combinator is the mu or Y combinator.
mu
where mu f x = f (mu f)
x
?data W a b = Wx(W a b -> a
-> b)
ww
where ww f (Wx g) x = f (g (Wx g)) x
?
mu2
where mu2 f x = ww f (Wx (ww f)) x
?
Note that the definition of mu2
only involves recursion
in that it uses the W
data type.
There is a theorem that says that in a typed language like
Haskell, you minimally need to use a
recursive data type to define mu
or mu2
.
In Scheme you can just write
(define ((w f g) x) ((f (g g)) x)
.
(there are three parts of this question that actually need answers.)
mu2
.)
factFact
where factFact fact n = if (n==(0::Int)) then 1 else toInteger n * fact(n-1);?
factFact
recursive? Answer No.factFact udefined 0
factFact udefined 1
(factFact . factFact $ udefined) 1
(factFact . factFact $ udefined) 2
(factFact . factFact . factFact $ udefined) 2
mu2 factFact(do not worry overly about the value above.)
mu2 factFact 5
[]
?drop 3 "cat"
?drop 3 ["cat"]
?[]==drop 3 "cat"
?
drop 3 ["cat"]==drop 3 "cat"
?
data VeryEmpty a = VoidThis has
Functor
,
Applicative
,
Monad
,
Foldable
, and
Traversable
instances.
(Some of these can be guessed from the definitions for the
Nothing
case of Maybe
.)
Define these instances. Be sure to test your code. It may be helpful to use
data VeryEmpty a = Void deriving Show
Tree a
type defined by
data Tree a = EmptyTree | Node a (Tree a) (Tree a)have a
Foldable
instance.
Tree a
type defined by
data Tree a = EmptyTree | Node a (Tree a) (Tree a)have a
Traversable
instance.
\ mm -> do { m <- mm ; m }
?
do
-block and make this as pointless as possible.uncurry
where uncurry f (a,b) = f a b
What does uncurry do?
Tree a
type defined by
data Tree a = EmptyTree | Node a (Tree a) (Tree a)have a
Functor
instance.
For the “Tree a
” discussed in class, write
a continuation-passing style tail-recursive algorithm
to mirror
a tree.
“Defunctionalize” your continuations.
What is an upper bound on the size of your continuations?
foldr
has a leftist cousin
foldl
defined by
foldl f a [] = a foldl f a (x:xs) = foldl f (f a x) xs
foldl
? How can you deduce
this without using ghci :type
?
foldl
can be written in terms of foldr
foldl f a xs = foldr (g f) id xs a where g …
What is the type of g
?
Write a definition for g
.
How “pointless” can you make g
?
Integer
to String
function can be found on
the Blacboard site.
Complete the code, or write your own from scratch, to provide a function to convert integers up to ??? into strings.
Winter 2022 | ||
2024-11 |
fall-2024