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

Bob Ippolito bob at redivi.com
Mon Apr 14 07:35:58 UTC 2014


You can't use guards in record syntax, but you could use `case` here
instead of `let`, or just write a function of type `String -> BuyOrSell`
and use that.


On Sun, Apr 13, 2014 at 11:03 PM, Dimitri DeFigueiredo <
defigueiredo at ucdavis.edu> wrote:

>
> I'm having some trouble understanding where I can or cannot use guards
> inside record syntax. I'm writing a simple conversion routine, but I am not
> able to write it without inserting an extra let. Do I need a let expression
> here? Am I missing something?
>
> --------------
> data OldTrade = OldTrade {
>     oldprice   :: Double   ,
>     oldamount  :: Double   ,
>     oldbuysell :: String  -- "buy", "sell" or ""
>     } deriving( Eq, Show)
>
>
> data BuyOrSell = Buy | Sell | Unknown deriving(Eq, Show)
>
> data Trade = Trade {
>     price   :: Double   ,
>     amount  :: Double   ,
>     buysell :: BuyOrSell
>     } deriving( Eq, Show)
>
> convert :: OldTrade -> Trade
>
> convert ot  = Trade { price   = oldprice  ot,
>                       amount  = oldamount ot,
>                       buysell = let x | oldbuysell ot == "buy"  = Buy
>                                       | oldbuysell ot == "sell" = Sell
>                                       | otherwise               = Unknown
>                                 in x
>                     }
>
> -- how do I eliminate the 'let' expression here?
> -- I wanted to write something like:
> --
> --                      buysell | oldbuysell ot == "buy"  = Buy
> --                              | oldbuysell ot == "sell" = Sell
> --                              | otherwise               = Unknown
>
> --------------
>
> Thanks!
>
> Dimitri
>
>
>
>
> _______________________________________________
> 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/e5c03100/attachment.html>


More information about the Beginners mailing list