Re: [Haskell-cafe] chr/ord?

Hi Yitz,
Thanks for your input.
Since I'm trying to learn Monads, let's look at this as a teaching moment. The example code (see below), which I pulled off YAMT (Yet Another Monad Tutorial ;-)), is the source of my 'comb' function.
I understand the code as it now stands, and I understand that the Prelude (>>=) would replace the 'comb'. Adding whatever statements are needed, how would you "specialize" the (>>=) to Maybe and solve this particular problem.
Michael
--- On Wed, 4/29/09, Yitzchak Gale
-- 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 reasons. 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. -Yitz

michael rice wrote:
Since I'm trying to learn Monads, let's look at this as a teaching moment. The example code (see below), which I pulled off YAMT (Yet Another Monad Tutorial ;-)), is the source of my 'comb' function.
I understand the code as it now stands, and I understand that the Prelude (>>=) would replace the 'comb'. Adding whatever statements are needed, how would you "specialize" the (>>=) to Maybe and solve this particular problem.
Saying that "comb is just (>>=) specialized to Maybe" just means that you can define comb like this: comb :: Maybe a -> (a -> Maybe b) -> Maybe b comb = (>>=) Which also of course means that you can typically use (>>=) instead of comb. Although in some cases, being more specific about the type can be useful. You can do this sort of specialization for any polymorphic function, e.g.: -- id is predefined in Haskell, definition given as example id :: a -> a id x = x intID :: Int -> Int intId = id In that case, the compiler basically specializes the function for you, providing a version of it that's specific to Ints. However, (>>=) is defined by the Monad type class, and as it happens there's also already a definition for it that's specific to the Maybe type. You can see GHC's source for it here: http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-Maybe.html#M... Not surprisingly, that definition is essentially identical to the definition of comb: (Just x) >>= k = k x Nothing >>= _ = Nothing So defining "comb = (>>=)" just uses that definition. Anton
participants (2)
-
Anton van Straaten
-
michael rice