3
votes

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)
3
You would get a much more helpful error if you had typed your functions - let this be a lesson for the future!alternative

3 Answers

13
votes

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)
2
votes

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"
0
votes

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