How do I use Guards in record syntax?

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

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@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Mon, Apr 14, 2014 at 1:03 PM, Dimitri DeFigueiredo < defigueiredo@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

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
On Mon, Apr 14, 2014 at 1:03 PM, Dimitri DeFigueiredo < defigueiredo@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Note that this has nothing to do with record syntax specifically. Rather, what you're asking is how to write a multi-way if expression. Your way is to introduce a local binding using a let statement, which allows you to use pattern guards as you did. Usually, bowever, you'd use a case statement to avoid the binding. However, you could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this thread. Or you could float out the binding to a where clause, except that doesn't seem to be what you're looking for. Hoping to help, Gesh

Thanks everyone :-) I think the "case of" is what I was looking for. I keep thinking of using "case of" as in pattern matching to find the "Shape" of the result of an expression and of using guards to evaluate predicates. Forgetting that True and False are constructors themselves. I just have to change that mindset. Cheers, Dimitri Em 14/04/14 09:04, Gesh escreveu:
On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners Note that this has nothing to do with record syntax specifically. Rather, what you're asking is how to write a multi-way if expression. Your way is to introduce a local binding using a let statement, which allows you to use pattern guards as you did. Usually, bowever, you'd use a case statement to avoid the binding. However, you could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this thread. Or you could float out the binding to a where clause, except that doesn't seem to be what you're looking for. Hoping to help, Gesh
Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

You wouldn't even need to match True and False, you could match the String you're looking for. convert ot = Trade { price = oldprice ot, amount = oldamount ot, buysell = case oldbuysell ot of "buy" -> Buy "sell" -> Sell _ -> Unknown } On Mon, Apr 14, 2014 at 11:36 AM, Dimitri DeFigueiredo < defigueiredo@ucdavis.edu> wrote:
Thanks everyone :-)
I think the "case of" is what I was looking for. I keep thinking of using "case of" as in pattern matching to find the "Shape" of the result of an expression and of using guards to evaluate predicates. Forgetting that True and False are constructors themselves. I just have to change that mindset.
Cheers,
Dimitri
Em 14/04/14 09:04, Gesh escreveu:
On April 14, 2014 9:03:20 AM GMT+03:00, Dimitri DeFigueiredo <
defigueiredo@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Note that this has nothing to do with record syntax specifically. Rather, what you're asking is how to write a multi-way if expression. Your way is to introduce a local binding using a let statement, which allows you to use pattern guards as you did. Usually, bowever, you'd use a case statement to avoid the binding. However, you could use the MultiWayIf LANGUAGE pragma, as suggested elsewhere in this thread. Or you could float out the binding to a where clause, except that doesn't seem to be what you're looking for. Hoping to help, Gesh _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Bob Ippolito
-
Dimitri DeFigueiredo
-
Gesh
-
Kim-Ee Yeoh
-
Tom Murphy