Re: [Haskell-cafe] chr/ord?

Thank guys,
Now what am I misunderstanding in the code below?
I would think that
*Main> comb (Just 65) foo
and
*Main> comb (lookup 'A' lst) foo
would return the same result
Just 'A'
Michael
===========Haskell code=============
import Data.Char
lst = [('A',65),('B',66),('C',67),('D',68)]
-- 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
foo :: Int -> Maybe Char
foo n = Just (chr n)
================Ghci===========
[michael@localhost ~]$ ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load test1
[1 of 1] Compiling Main ( test1.hs, interpreted )
Ok, modules loaded: Main.
*Main> comb (Just 65) foo
Just 'A'
*Main> comb Nothing foo
Nothing
*Main> lookup 'A' lst
Just 65
*Main> (lookup 'A' lst)
Just 65
*Main> comb (lookup 'A' lst) foo
<interactive>:1:22:
Couldn't match expected type `Integer' against inferred type `Int'
In the second argument of `comb', namely `foo'
In the expression: comb (lookup 'A' lst) foo
In the definition of `it': it = comb (lookup 'A' lst) foo
*Main>
--- On Tue, 4/28/09, Lennart Augustsson

On Apr 28, 2009, at 23:32 , michael rice wrote:
Thank guys,
Now what am I misunderstanding in the code below?
lst = [('A',65),('B',66),('C',67),('D',68)]
You didn't give a type for lst, so it defaulted to [(Char,Integer)]. This is a manifestation of the Monomorphism Restriction, invoked because lst doesn't take any arguments: it forces lst to have a definite type, so defaulting is used to fix the numeric part to Integer. If you add a type specification for lst, things will work as you expect. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (2)
-
Brandon S. Allbery KF8NH
-
michael rice