| This is a course page of David Casperson You are here: home → Semesters → Fall 2023 → CPSC 370… |
|
…can be found at ./370/homework/pending.php .
Bool functions are there?foldl and no
explicit tail-recursiion.
Write a function
isSorted::(Ord a) => ArrayNE a -> Bool
that returns True iff its argument is sorted in
non-decreasing (≤) order.
Hoogle
the Down newtype. What is it for?
Show how to use your previous
isSorted function
for ArrayNEs, Down, and fmap
to determine if an ArrayNE is sorted in
non-increasing (≥) order.
With a newtype wrapper
newtype Cx = Cx { getCs :: Int }
around Int, is
instance Semigroup Cx where
x <> y = Cx (1 + getCx x)
a legitimate semigroup definition? Why or why not?
data OneValueType = ThisIsIt
instance Semigroup OneValueType where
(<>) = const
instance Monoid OneValueType where
mempty = ThisIsIt
determine if this is a legitimate mathematical monoid.
(yes, this example is perverse.)
data Triple a = Tx a a a”, define
Functor,Foldable, andApplicativeTriple.
data RoseTree a = Pure a | Nest [RoseTree a]define
Functor and
Foldable instances for RoseTree.
Install Prolog on your computer. (Preferably SWI Prolog.) Tell me how you did this, including which Prolog version, and the operating system for your computer.
Question Numbering below fixed 2023-10-04.
Write a definition for the uncurry function.
Yes, it is possible to Hoogle the answer.
The preferred approach is to look hard at your answers to
Question 14-(7,8) until you are sure you know what
uncurry
is doing.
Next, climb a 10 000' mountain. Ignore the sweat dripping into your eyes, and obtain a state of Zen-like calm. Wait for the solution to arrive.
When it doesn’t, climb back down the mountain. Be sure to have a pencil and piece of paper with you. When the answer suddenly occurs to you, write it down.
When you get home, try it out in ghci.
maybe fun?
The Maybe type gets used a lot, partially because it
provides a way to make partial functions total, partially because
Maybe is an instance of important classes like
Monad and Applicative.
Consequently there are a lot of small, but useful functions for
Maybe. Many of them can be written wusing
maybe (that's the function, not the type).
For each of the following maybe expressions;
:type)
Try these:
maybe Nothing Justmaybe Nothing (Just . f) (Here f must be a
function. Try specific functions to see if you can get a
pattern.)
flip maybe idmaybe Nothing idmaybe True (const False). Also, what does
const do? Why is it necessary here?maybe [] (:[]). Let's start with some questions where we mimic lists with
the
Array2
and
ArrayNE
from the class notes.
There is a module
JoinList
in the Moodle notes. Download it, and import it into your own code.
Many of these questions can be solved indirectly with code that
looks like
fromList . a List function
. toList
.
Avoid doing so.
Most of the solutions are (mecessarily) recursive. Tail-recursive solutions are not necessary.
take
and
drop
functions that work like their list equivalentgs,
but on Array2's. Be careful if you write a
dropNE function because you may end up with an empty
array.
map
function that work like its list equivalentgs,
filter :: (a -> Bool) -> (Array2 a -> Array2 a)
function that keeps only the elements that satisfy the
(a -> Bool) predicate.
where”
and
“let … in …”.
Using the notion of guards from Chapter 3, wrote code to compute
the sin function using the formulas on
this page.
Hote that you can ignore everything after the
word Bonus.
The point of this question is to learn how to use guard
notation (and possibly let or where).
If you are having difficulty reading the math notation, ask me!
Question Numbering below fixed 2023-10-04.
Think about exch expression. See if you can work out in your head what the type is (or determine that there is something wrong).
Use ghci to determine which of these expressions
are legal. For the legal values, use
“:type” to determine what the type of
the expression is.
Do any of the results surprise you?
The arc hyperbolic cosine is defined by
arc cosh y = log ( y ± sqrt ( y^2 - 1))
Use the totalSqrt and totalLog defined
in the previous question to write a totalArcCosh :: Double
-> Maybe Double
function. The point of this question is to experience using
functions like totalSqrt. It is possible to
code a function
totalArcCosh :: Double -> Maybe Double
directly, but that is not the point of this question.
(The arc cosh is the inverse of
the cosh function, defined by
cosh x = ((exp x) + (exp (-x)))/2 .
You can use this fact to test your arc cosh function:
you should have (arc cosh (cosh x)) = x.)
totalDot function:
totalDot :: (b -> Maybe c)
-> (a -> Maybe b)
-> (a -> Maybe c)
that is the analog of standard function compositon (.)
(.) :: (b -> c) -> (a -> b) -> (a -> c)With this function you should be able to write formulæ like
totalDot totalSqrt totalLog x .
Code this reasonably directly. The following questions will show
slicker and shorter ways to code this idea.
maybe function.
maybe?maybe to give very
consice defintion
of totalDot?totalDot.
(You will need an import to use the operator, but it is part of
the standard Haskell install.)
sqrt x is not defined for
x < 0;
log x is not defined for
x ≤ 0;
Define functions
totalSqrt :: Double -> Maybe Double
totalLog :: Double -> Maybe Double
that have as large a domains as possible.
Use ghci to determine which of these expressions
are legal. For the legal values, use
“:type” to determine what the type of
the expression is.
Do any of the results surprise you?
Ord class?
/* … */”) do not nest.
public class Oooops {
/* /* Don’t try this */ at home */
}
What is the haskell syntax for block
comments? Do they nest?
![]() |
|
|
![]() |
Fall 2023 |
|
| 2025-10 |
|
fall-2024