[Haskell-beginners] How do I use Guards in record syntax?
Bob Ippolito
bob at redivi.com
Mon Apr 14 18:44:03 UTC 2014
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 at 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 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
>>>
>> 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 at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
> _______________________________________________
> 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/e47bb9b4/attachment.html>
More information about the Beginners
mailing list