Homework #13 Questions

Homework Questions

Q: What is the syntax of instance declarations?

A: OK.

instance constraint Class type where
  declarations of overloaded functions
(The declarations MUST be indented.) as in
instance (Eq a) => Eq (Tree a) where
  Leaf a         == Leaf b          =  a == b
  (Branch l1 r1) == (Branch l2 r2)  =  (l1==l2) && (r1==r2)
  _              == _               =  False
where constraint is ``(Eq a) =>'' (and may be empty if there are no constraints), Class is ``Eq'', type is ``Tree a'' and the body here defines function == using three equations.

Q What does it mean that our definition of mystery cannot ``create any of its own monadic values'' ?

A: It should not call ``return.''

Comments/Corrections

The polynomials question says ``Then define make [a] an instance of Num...'' It shoudl say ``Then make [a] an instance of Num...''

Input/Output Question 3 has a typo in the program. It should be:

filter(\c -> c /= 'Q')
The printed homework has "." where this has "->".

A student has noticed that you can ask ":type expr" to find the type of an expression (like SML did). For example:

Prelude> :type \x -> x + 1
\x -> x + 1 :: Num a => a -> a
Notice that with SML, it would assume we were working with integers. here it works for any type which is an instance of the Num class.

There is no homework13 directory. Just create it. I can do it for you if you desire. Ket me know soon.

The last ``let'' expression to examine for ``Lazy Evaluation'' ended with end. This was an ML-inspired typo. Please ignore it.

General Haskell Questions

Q: Is Num a predefined type class?

A: Yes, see tutorial (Section 10)

Q: What does fromInteger do?

A: It permits the integer constants to be overloaded by any instance of the Num class.

Q: What does the symbol !! mean?

A: 0-based element of a list. z!!2 means the second element of the list in z

Q: What is a ``monadic value''?

A: Something of type IO X for some type X.

Q: Please remind me of what >>= does:

A:

   (>>=)            :: IO a -> (a -> IO b) -> IO b
This takes the I/O action, and returns a new I/O action that when performed runs the first action and thwn feed its result to the function to get a new action, which is then performed.