[Haskell-beginners] Typeclasses vs. Data

Felipe Almeida Lessa felipe.lessa at gmail.com
Thu Jul 21 05:27:26 CEST 2011


On Wed, Jul 20, 2011 at 8:41 PM, Thomas <haskell at phirho.com> wrote:
> eval_begin :: Continuation a => Int -> a -> Int
> eval_begin n k = eval n (if (n < 0) then k else (BeginCont k (n - 1)))

Although 'eval n' is polymorphic, the if expression needs to have just
one type, either 'a' or 'BeginCont a', and they can't be unified.  The
solution is pretty simple, though, since eval's return type doesn't
mention 'a' at all:

  eval_begin :: Continuation a => Int -> a -> Int
  eval_begin n k = if (n < 0) then eval n k else eval n (BeginCont k (n - 1))

Note that 'eval n' is repeated.  If you don't to repeat it on your
real world code you may give it an explicit name.  However you'll need
to provide a type signature from GHC 7.0 onwards:

  eval_begin :: Continuation a => Int -> a -> Int
  eval_begin n k =
    let eval' :: Continuation a => a -> Int
        eval' = eval n
    in if (n < 0) then eval' k else eval' (BeginCont k (n - 1))

HTH, =)

-- 
Felipe.



More information about the Beginners mailing list