0

Here is my code:

> newtype IN_0 = IN_0 Integer deriving Show

> nminus :: IN_0 -> IN_0 -> IN_0
> nminus (IN_0 z) (IN_0 z')
>        | z >= 0 && z' >= 0 = IN_0 $ maximum(0,z-z')
>        | otherwise = IN_0 0

When I compile this in ghci it compiles without error, but when I try to compile it in hugs I get this error message:

ERROR "untitled.lhs":51 - Type error in application
*** Expression     : maximum (0,z - z')
*** Term           : (0,z - z')
*** Type           : (b,Integer)
*** Does not match : [a]

Why ?

cheshire
  • 1,109
  • 3
  • 15
  • 37
  • 2
    if you're using prelude's maximum you don't even need to use it and just have `(z-z')` – cmdv Nov 07 '18 at 16:49
  • 2
    This doesn’t directly answer your question, but it may add some additional context: it’s probably worth pointing out that Hugs hasn’t been updated in more than a decade, while GHC is actively developed. – David Young Nov 07 '18 at 16:55
  • 3
    You want to use `max 0 (z - z')`. `maximum` finds the largest value in a list, not the larger of two arguments. – chepner Nov 07 '18 at 16:58

1 Answers1

0
> newtype IN_0 = IN_0 Integer deriving Show

> nminus :: IN_0 -> IN_0 -> IN_0
> nminus (IN_0 z) (IN_0 z')
>        | z >= 0 && z' >= 0 = IN_0 $ maximum [0,z-z']
>        | otherwise = IN_0 0

In Hugs you have to use [Square] brackets when using maximum

cheshire
  • 1,109
  • 3
  • 15
  • 37
  • 3
    In order for it to work the way that you want, you must do the same in GHC. The version of `maximum` you want takes in a list (which is given with square braces). In GHC, `maximum` is generalized to work over any `Traversable`. Pair types are a kind of `Traversable`, but not the kind you want: due to how the `Traversable` instance for pairs work, `maximum (a,b)` will *always* be `b`. The reason for this is probably outside the scope of this question (it is a controversial decision though). What you should really use for finding the maximum of two values, as suggested by @chepner, is `max a b`. – David Young Nov 07 '18 at 18:28