[Haskell-cafe] chr/ord?

michael rice nowgate at yahoo.com
Wed Apr 29 11:49:19 EDT 2009

Oops, here's the code.



{- Author:     Jeff Newbern
   Maintainer: Jeff Newbern <jnewbern at nomaware.com>
   Time-stamp: <Mon Nov 10 11:59:14 2003>
   License:    GPL


Example 1 - Our first monad

Usage: Compile the code and execute the resulting program.
       It will print Dolly's maternal grandfather.

-- everything you need to know about sheep
data Sheep = Sheep {name::String, mother::Maybe Sheep, father::Maybe Sheep}

-- we show sheep by name

instance Show Sheep where
  show s = show (name s)

-- comb is a combinator for sequencing operations that return Maybe

comb :: Maybe a -> (a -> Maybe b) -> Maybe b
comb Nothing  _ = Nothing
comb (Just x) f = f x

-- now we can use `comb` to build complicated sequences
maternalGrandfather :: Sheep -> Maybe Sheep
maternalGrandfather s = (Just s) `comb` mother `comb` father

fathersMaternalGrandmother :: Sheep -> Maybe Sheep
fathersMaternalGrandmother s = (Just s) `comb` father `comb` mother `comb` mother 

mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = (Just s) `comb` mother `comb` father `comb` father 

-- this builds our sheep family tree
breedSheep :: Sheep
breedSheep = let adam   = Sheep "Adam" Nothing Nothing
                 eve    = Sheep "Eve" Nothing Nothing
         uranus = Sheep "Uranus" Nothing Nothing
         gaea   = Sheep "Gaea" Nothing Nothing
         kronos = Sheep "Kronos" (Just gaea) (Just uranus)
                 holly  = Sheep "Holly" (Just eve) (Just adam)
             roger  = Sheep "Roger" (Just eve) (Just kronos)
             molly  = Sheep "Molly" (Just holly) (Just roger)
         in Sheep "Dolly" (Just molly) Nothing

-- print Dolly's maternal grandfather
main :: IO ()
main = let dolly = breedSheep
       in do print (father dolly)

--- On Wed, 4/29/09, Yitzchak Gale <gale at sefer.org> wrote:

From: Yitzchak Gale <gale at sefer.org>
Subject: Re: [Haskell-cafe] chr/ord?
To: "michael rice" <nowgate at yahoo.com>
Cc: "Brandon S. Allbery KF8NH" <allbery at ece.cmu.edu>, "haskell-cafe at haskell.org" <haskell-cafe at haskell.org>
Date: Wednesday, April 29, 2009, 4:30 AM

Michael Rice wrote:
> -- comb is a combinator for sequencing operations that return Maybe
> comb :: Maybe a -> (a -> Maybe b) -> Maybe b
> comb Nothing  _ = Nothing
> comb (Just x) f = f x

comb is essentially the same as something in the Prelude:
it is just (>>=) specialized to Maybe.

(>>=) :: Monad m => m a -> (a -> m b) -> m b

> Now what am I misunderstanding in the code below?
> lst = [('A',65),('B',66),('C',67),('D',68)]

Brandon S. Allbery wrote:
> ...it defaulted to [(Char,Integer)].  This is a manifestation
> of the Monomorphism Restriction...

While it may be debatable whether the Monomorphism
Restriction is helpful in compiled code, it is unquestionably
a major nuisance at the GHCi prompt, for this and other

I highly recommend that you create a .ghci file in your home
directory containing the line:

:set -XNoMonomorphismRestriction

In my opinion, MR should be off by default in GHCi.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090429/fadb1f08/attachment.htm

More information about the Haskell-Cafe mailing list