Haskell, error unwrapping Maybe Int -
i wrote code:
is3 :: int -> maybe int is3 x = is3temp 0 x is3temp :: int -> int -> maybe int is3temp p x = if (abs p*p*p) < (abs x) (is3temp (p+(signum x)) x) else (if p*p*p == x (just p) else nothing) c :: maybe int -> int c (just x) = 2*x+1 c nothing = 0 --fun::int -> int --fun = c.is3 c1:: int -> int c1 x = 2*x +1 c1 0 = 0 fun::int -> int fun x = (is3 x) >>= c1 as can see is3 takes int , returns maybe int.
in fun take result is3 unwrap , try send c1.
and error
error file:1627.hs:47 - type error in application *** expression : is3 x >>= c1 *** term : c1 *** type : int -> int *** not match : -> b c what wrong here?
let's inspect types come in is3 x >>= c1:
x :: int is3 :: int -> maybe int c1 :: int -> int ||| /???\ is3 x :: maybe int ||| ??????? (>>=) :: maybe -> (a -> maybe b) -> maybe b (>>=) is3 x :: (int -> maybe b) -> maybe b is3 x >>= c1 :: $!%&/#? as can see, c1 doesn't work right hand side of >>=, since it's return type wrong. must return kind of maybe. 1 can fix return . c1:
return . c1 :: int -> maybe int or fmap, since monad laws dictate both must have same effect:
fmap c1 :: maybe int -> maybe int fmap c1 (is3 x) :: maybe int however, change fun's type. being said, functions name rather bad , c1 typo (if fun's type correct). it's possible wanted use c:
fun :: int -> int fun x = c (is3 x)
Comments
Post a Comment