[Haskell-beginners] How do I use Guards in record syntax?
Dimitri DeFigueiredo
defigueiredo at ucdavis.edu
Mon Apr 14 06:03:20 UTC 2014
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
More information about the Beginners
mailing list