129

I have three functions that find the nth element of a list:

nthElement :: [a] -> Int -> Maybe a 
nthElement [] a = Nothing
nthElement (x:xs) a | a <= 0 = Nothing
                    | a == 1 = Just x
                    | a > 1 = nthElement xs (a-1)

nthElementIf :: [a] -> Int -> Maybe a
nthElementIf [] a = Nothing
nthElementIf (x:xs) a = if a <= 1
                        then if a <= 0 
                             then Nothing
                             else Just x -- a == 1
                        else nthElementIf xs (a-1)                           

nthElementCases :: [a] -> Int -> Maybe a
nthElementCases [] a = Nothing
nthElementCases (x:xs) a = case a <= 0 of
                             True -> Nothing
                             False -> case a == 1 of
                                        True -> Just x
                                        False -> nthElementCases xs (a-1)

In my opinion, the first function is the best implementation because it is the most concise. But is there anything about the other two implementations that would make them preferable? And by extension, how would you choose between using guards, if-then-else statements, and cases?

2
  • 5
    you can collapse your nested case statements if you used case compare a 0 of LT -> ... | EQ -> ... | GT -> ...
    – rampion
    Commented Feb 19, 2012 at 2:11
  • 5
    @rampion: you mean case compare a 1 of ...
    – newacct
    Commented Feb 19, 2012 at 4:39

4 Answers 4

150

From a technical standpoint, all three versions are equivalent.

That being said, my rule of thumb for styles is that if you can read it as if it was English (read | as "when", | otherwise as "otherwise" and = as "is" or "be"), you're probably doing something right.

if..then..else is for when you have one binary condition, or one single decision you need to make. Nested if..then..else-expressions are very uncommon in Haskell, and guards should almost always be used instead.

let absOfN =
  if n < 0 -- Single binary expression
  then -n
  else  n

Every if..then..else expression can be replaced by a guard if it is at the top level of a function, and this should generally be preferred, since you can add more cases more easily then:

abs n
  | n < 0     = -n
  | otherwise =  n

case..of is for when you have multiple code paths, and every code path is guided by the structure of a value, i.e. via pattern matching. You very seldom match on True and False.

case mapping of
  Constant v -> const v
  Function f -> map f

Guards complement case..of expressions, meaning that if you need to make complicated decisions depending on a value, first make decisions depending on the structure of your input, and then make decisions on the values in the structure.

handle  ExitSuccess = return ()
handle (ExitFailure code)
  | code < 0  = putStrLn . ("internal error " ++) . show . abs $ code
  | otherwise = putStrLn . ("user error " ++)     . show       $ code

BTW. As a style tip, always make a newline after a = or before a | if the stuff after the =/| is too long for one line, or uses more lines for some other reason:

-- NO!
nthElement (x:xs) a | a <= 0 = Nothing
                    | a == 1 = Just x
                    | a > 1 = nthElement xs (a-1)

-- Much more compact! Look at those spaces we didn't waste!
nthElement (x:xs) a
  | a <= 0    = Nothing
  | a == 1    = Just x
  | otherwise = nthElement xs (a-1)
6
  • 1
    "You very seldom match on True and False" is there any occasion at all where you would do that? After all, this kind of decision can always be done with an if, and with guards also. Commented Feb 19, 2012 at 12:01
  • 4
    E.g. case (foo, bar, baz) of (True, False, False) -> ...
    – dflemstr
    Commented Feb 19, 2012 at 12:11
  • @dflemstr Aren't there any more subtle differences e.g. guards requiring MonadPlus and and return an instance of monad whilst if-then-else does not? But I am not sure.
    – J Fritsch
    Commented Feb 19, 2012 at 17:27
  • 2
    @JFritsch: the guard function requires MonadPlus, but what we are talking about here is guards as in | test = clauses, which aren't related. Commented Mar 1, 2012 at 14:58
  • Thanks for the style tip, now it confirmed by doubt.
    – daparic
    Commented Aug 1, 2018 at 17:33
24

I know this is question about style for explicitly recursive functions, but I would suggest that the best style is finding a way to reuse existing recursive functions instead.

nthElement xs n = guard (n > 0) >> listToMaybe (drop (n-1) xs)
0
6

While all three implementations produce correct results, GHC (as of year 2021) complains that pattern matches are non-exhaustive – which is true insofar as possible patterns are hidden behind guards/if/case. Consider this implementation, which is both more conscise than the three of them, plus avoids a non-exhaustive patterns warning:

nthElement :: [a] -> Int -> Maybe a
nthElement (x:_) 1  = Just x
nthElement (_:xs) i = nthElement xs (i - 1)
nthElement _ _      = Nothing  -- index is out of bounds

The last pattern matches everything and therefore needs to be below a possibly successful match from the first two patterns.

4

This is just a matter of ordering but I think its very readable and has the same structure as guards.

nthElement :: [a] -> Int -> Maybe a 
nthElement [] a = Nothing
nthElement (x:xs) a = if a  < 1 then Nothing else
                      if a == 1 then Just x
                      else nthElement xs (a-1)

The last else doesn't need an if since there are no other possibilities, also functions should have "last resort case" in case you missed anything.

1
  • 6
    Nested if-statements are an anti-pattern when you can use case guards.
    – user76284
    Commented May 22, 2017 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.