• =?UTF-8?B?SGFza2VsbCBxdWVzdGlvbg==?=

    From =?UTF-8?B?QmxhY2sgU3dhbg==?=@21:1/5 to All on Mon Jul 16 17:57:43 2018
    I'm struggling with the following function:

    The following declaration works:

    data DividedResult = Result Integer | DividedByZero deriving (Eq, Show)

    dividedBy num denom =
    let
    (count,remainder,success) = x num denom 0
    in if success then (Result count) else DividedByZero
    where
    x num denom count
    | denom == 0 = (0,0,False)
    | num < denom = (count,num,True)
    | otherwise = x (num - denom) denom (count + 1)


    When I check the type of dividedBy, I get

    *Lib> :t dividedBy
    dividedBy :: (Num b, Ord b) => b -> b -> DividedResult

    However, if I try to add the type declaration line:

    dividedBy :: (Num a, DividedResult b) => a -> a -> b

    The module fails to load, yielding the following error:

    Expected a constraint, but DividedResult has kind *
    In the type signature:
    dividedBy :: (Num a, DividedResult b) => a -> a -> b
    |
    10 | dividedBy :: (Num a, DividedResult b) => a -> a -> b
    | ^^^^^^^^^^^^^
    Failed, no modules loaded.

    Could someone please kindly point out my mistake and suggest a type declaration line that works?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Florian Weimer@21:1/5 to All on Mon Jul 16 23:07:25 2018
    * Black Swan:

    When I check the type of dividedBy, I get

    *Lib> :t dividedBy
    dividedBy :: (Num b, Ord b) => b -> b -> DividedResult

    However, if I try to add the type declaration line:

    dividedBy :: (Num a, DividedResult b) => a -> a -> b

    These two types are not the same. Why do you think the second type
    would work? There are two problems: DividedResult doesn't take a type parameter, and functions of types as general as Num a => a -> b do not
    really exist.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?B?QmxhY2sgU3dhbg==?=@21:1/5 to All on Tue Jul 17 15:07:40 2018
    On Mon Jul 16 23:07:25 2018 Florian Weimer wrote:
    * Black Swan:

    When I check the type of dividedBy, I get

    *Lib> :t dividedBy
    dividedBy :: (Num b, Ord b) => b -> b -> DividedResult

    However, if I try to add the type declaration line:

    dividedBy :: (Num a, DividedResult b) => a -> a -> b

    These two types are not the same. Why do you think the second type
    would work? There are two problems: DividedResult doesn't take a type parameter, and functions of types as general as Num a => a -> b do not
    really exist.

    Got it! Thank you!

    I was confused between type and class. I should have declare the function as:

    dividedBy :: (Num n, Ord n) => n -> n -> DividedResult

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?B?QmxhY2sgU3dhbg==?=@21:1/5 to All on Tue Jul 17 15:01:29 2018
    On Mon Jul 16 23:07:25 2018 Florian Weimer wrote:
    * Black Swan:

    When I check the type of dividedBy, I get

    *Lib> :t dividedBy
    dividedBy :: (Num b, Ord b) => b -> b -> DividedResult

    However, if I try to add the type declaration line:

    dividedBy :: (Num a, DividedResult b) => a -> a -> b

    These two types are not the same. Why do you think the second type
    would work? There are two problems: DividedResult doesn't take a type parameter, and functions of types as general as Num a => a -> b do not
    really exist.

    Oh, my bad. Sorted with:

    dividedBy :: (Num n, Ord n) => n -> n -> DividedResult

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)