dramforever•

Speaking of fake keywords, I wrote this a while ago and it surprises me as each of the following three functions makes sense on its own

if_ = guard
then_ = ($>)
else_ = flip fromMaybe

But when combined gives a neat approximation of if-then-else where every sub-expression means something:

if_ cond -- Put them in your do for Maybe for condition check
if_ cond `then_` value -- Optional value?
if_ cond `then_` value `else_` alternate -- Not optional anymore
14
Zemyla•

What precedence makes this work right with chained conditions? Like:

`if_ a `then_` b `else_` if_ c `then_` d `else_` e
3
dbaynard•

else_ is also ?: from errors.

2
edwardkmett•

do can be used to wrap any expression

Prelude> 1 * do 2 + 3
5

With BlockArguments you will be able to do the

while cond do
  whatever

example right out of the box!

You still need to use this trick for the rest, though. ;)

12
nomeata•

Hooray for BlockArguments

7
g__•

In Agda, you can define a mixfix operator, e.g.

if_then_else_ : {A : Set} -> Bool -> A -> A -> A
if true  then t else f = t
if false then t else f = f

or while_do_ likewise.

There are also syntax declarations which allow to define custom binding structures.

8
bheklilr•

I've definitely done this before, but only in hobby projects a long time ago. It's not unique to haskell though, I picked it up from lisp where it's been around probably longer than I have.

5
davidfeuer•

It's even in R5RS Scheme's minimalist syntax-rules macro system.

3
twistier•

I'd rather use newtypes around the arguments I want to label.

3
haxly•

esqueleto does a simpler version of this to emulate the sytnax of case expressions:

case_
  [ when_ <condition> then_ <value>
  , ...
  ]
  (else_ <alternative>)

when_ has three arguments. The second argument is of type unit, and then_ is an alias for ()

2
davidfeuer•

Along with the idiom bracket example, you can look at the HList package, which provides similar "syntax" for heterogeneous lists.

2