Beta

The King lives! the Elvis operator

Description:

In other programming languages,

The King lives!

Let's add the Elvis operator to Haskell

(?:) :: ("SortOfBoolean" a) => a -> a -> a
left ?: right | left      = left
              | otherwise = right

If left is truthy, we get left
If left is falsy, we get right

Truthy and falsy values

Falsy values are, for starters:

  • False :: Bool
  • () :: ()
  • 0 :: (Num a) => a
  • "" :: String

Non-falsy values are, well, truthy. You will need to define (?:) for all of these types and their values

All sorts of algebraic datatypes can also be truthy / falsy:

  • [] :: [a] is falsy; any non-empty list of a is truthy
  • Nothing :: Maybe a is falsy; any Just a is truthy
  • any Left a :: Either a b is falsy; any Right b is truthy
  • (a,b) is falsy iff b is falsy ( cf. Functor, same as Maybe and Either really )

From Data.Ord:

  • Down a is falsy iff a is falsy. Order does not affect truthfulness ( or something )

From Data.Monoid:

  • Dual a is falsy iff a is truthy (!) This one might actually come in handy sometime
    ( We skip the rest of the Monoid wrappers; they don't really add anything )

You will also need to define (?:) for all of these types and their values

Non-strictness

In keeping with normal Haskell behaviour:

in left ?: right, right must not be evaluated if left is truthy ( "short-circuiting" )
in Just a, Left a and Right b, a and b must not be evaluated
in (a,b), a must not be evaluated
in general, avoid needlessly evaluating values

Notes

Char does not really have a falsy value ( <NUL> just does not compute ); there will be no tests with Char s
In Num, Int Integer Word Float Double and the algebraic Ratio and Complex will be tested, but not Int8 Word8 and friends

This kata started as a problem, not a solution. I have not looked deeply into Functor Applicative Monad Foldable and Traversable types and values ( yet ). There may be room for a sequel. Suggestions are welcome.

Fundamentals

Similar Kata:

Stats:

CreatedNov 5, 2018
PublishedNov 5, 2018
Warriors Trained89
Total Skips23
Total Code Submissions128
Total Times Completed24
Haskell Completions24
Total Stars4
% of votes with a positive feedback rating68% of 14
Total "Very Satisfied" Votes6
Total "Somewhat Satisfied" Votes7
Total "Not Satisfied" Votes1
Total Rank Assessments14
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • JohanWiltink Avatar
Ad