Haskell guards on lambda functions?

Viewed 21962

Is it possible to have guards on lambda functions?

For example:

\k
    | k < 0     -> "negative"
    | k == 0    -> "zero"
    | otherwise -> "positive"
5 Answers

Yet another way uses a combination of the LambdaCase and ViewPatterns GHC extensions:

{-# LANGUAGE LambdaCase, ViewPatterns #-}

\case
    ((< 0) -> True) -> "negative"
    ((==0) -> True) -> "zero"
    _               -> "positive"

This keeps you from having to name an intermediate variable.

Related