Couldn't match expected type 'Bool' with type 'm Bool'

Two module Up.hs and Down.hs

module Up (isSortedUp) where
isSortedUp x y z = if x>y && y>z then return(True) else return(False)


module Down (isSortedDown) where
isSortedDown x y z = if x<y && y<z then return(True) else return(False)

And the main program Main.hs

import System.Environment
import Up
import Down
main = do
  args<-getArgs
  let a = read(args !! 0)
  let b = read(args !! 1)
  let c = read(args !! 2)
  if (isSortedUp a b c || isSortedDown a b c) 
    then putStrLn "True" 
    else putStrLn "False"

During compilation I get the following error:

Couldn't match expected type `Bool' with actual type `m0 Bool'
In the return type of a call of `isSortedUp'
In the first argument of `(||)', namely `isSortedUp a b c '
In the expression: (isSortedUp a b c || isSortedDown a b c)

Upvotes: 3

Views: 4493

Answers (3)

hammar
hammar

Reputation: 139840

You seem to be confused about return. It is not a keyword for returning values like in other programming languages. In Haskell, return is a function that promotes a pure value to a monadic one (e.g. an Int to an IO Int). You don't use it for non-monadic code.

isSortedUp x y z = if x>y && y>z then True else False

Also, instead of writing if foo then True else False, you can simply write foo:

isSortedUp x y z = x>y && y>z

Your main function can also be simplified a little using pattern matching and the fact that print on booleans prints "True" or "False".

main = do
  (a:b:c:_) <- getArgs
  print (isSortedUp a b c || isSortedDown a b c)

Upvotes: 13

Erik Hinton
Erik Hinton

Reputation: 1946

I don't believe that you need the "return"s in your functions. As the isSorted functions don't return out to the monad, they simply evaluate the functions and therefore do not need to be wrapped up (and that's what return does). Also you can simplify your let statements with some deconstruction.

I would suggest trying:

import System.Environment

isSortedUp x y z = x>y && y>z
isSortedDown x y z = x<y && y<z

main = do
  args<-getArgs
  let (a:b:c:xs) = args
  if ((isSortedUp a b c) || (isSortedDown a b c)) then putStrLn "True" else putStrLn "False"

Upvotes: 2

Daniel Wagner
Daniel Wagner

Reputation: 152707

Don't call the return function in your isSorted functions.

Upvotes: 0

Related Questions