looking for data structure advice

Samuel Tardieu sam at rfc1149.net
Mon Dec 15 13:50:47 EST 2003


>>>>> "David" == David Roundy <droundy at abridgegame.org> writes:

David> My or_maybe is just defined as

David> or_maybe (Just e) _ = Just e or_maybe Nothing f = f

David> which is pretty simple, so you can run a series of calculations
David> (assuming you want to keep the first that has a non-Nothing
David> result) using

David> f = a `or_maybe` b `or_maybe` c

The advantage of using "mplus" rather than "or_maybe" is that you can
write:

  f = a `mplus` b `mplus` c

and that it will also work with other MonadPlus instances. For
example, if you decide that you want to keep several results and work
with "[a]" rather than "Maybe a", your "f" function will work as-is
and will concatenate the different results.

While "Maybe a" holds zero or one value, "[a]" holds zero or more
values. Switching between both can something be very practical for
functions reuse.

  Sam
-- 
Samuel Tardieu -- sam at rfc1149.net -- http://www.rfc1149.net/sam



More information about the Haskell-Cafe mailing list