[Haskell-beginners] How do I use Guards in record syntax?

Tom Murphy amindfv at gmail.com
Mon Apr 14 12:22:41 UTC 2014


The closest thing to guards that'll work is multi-way-if (you'll have to
put "{-# LANGUAGE MultiWayIf #-}" at the top of your source file)

e.g.

buysell = if | oldbuysell ot == "buy"  = Buy
                  | oldbuysell ot == "sell" = Sell
                  | otherwise               = Unknown

but a 'case' is slightly shorter here:


buysell = case oldbuysell ot of
   "buy"  -> Buy
   "sell" -> Sell
   _ -> Unknown


Tom


On Mon, Apr 14, 2014 at 4:22 AM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:

>
> On Mon, Apr 14, 2014 at 1:03 PM, Dimitri DeFigueiredo <
> defigueiredo at ucdavis.edu> wrote:
>
>>     oldbuysell :: String  -- "buy", "sell" or ""
>
>
> Why is this String when you've defined the perfectly cromulent BuyOrSell
> datatype?
>
> How can you be sure there won't be some accidental buggy code that sets
> the field to "buyy"?
>
> -- Kim-Ee
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140414/5401042e/attachment.html>


More information about the Beginners mailing list