[Haskell-beginners] Lifting over record syntax

Jaakko Luttinen jaakko.luttinen at iki.fi
Sun Oct 14 07:15:06 UTC 2018


Hi!

Let's consider this simple data structure:

data Person = Person {name::String, age::Int} deriving Show

Now, I can create maybe-people like in applicative style:

Person <$> Just "John Doe" <*> Nothing

or in monad style:

do
   name' <- Just "John Doe"
   age' <- Nothing
   return Person {name=name', age=age'}

The problem with the first approach is that it depends on the order of 
the arguments because it doesn't utilize the record syntax. The problem 
with the second approach is that there's a bit of unnecessary 
boilerplate (e.g., name' and age' variables).

I would like to get the benefits of the the record syntax but with 
similar code simplicity as the applicative style has. Do you have ideas 
is this possible? My non-working pseudo-code would look like:

Person <$> {name=Just "John Doe", age=Nothing}

But this doesn't work, it's syntax error.

Any ideas for a nice syntax?

Cheers,
Jaakko


More information about the Beginners mailing list