[Haskell-beginners] small question
Bastian Erdnüß
earthnut at web.de
Tue Oct 19 14:35:44 EDT 2010
On Oct 19, 2010, at 16:13, Mohamed wrote:
> Hello folks,
>
> just a quickie here and would appreciate any help...
>
> In this expression:
>
>
>
> instance OBSERVATIONS Drinkability WaterWell Volunteer where
> observe (Drinkability waterWell) volunteer = volunteer {vReport =
> if (odorQuale (perceive (Odor waterWell) volunteer) == True &&
> clarityQuale (perceive (Clarity waterWell) volunteer) == True && fullnessQuale
> (perceive (Fullness waterWell) volunteer) == True) then (if (honesty) then
> "drinkable" else "undrinkable") else (if (honesty) then "undrinkable" else
> "drinkable")}
I suppose this could also be written
instance OBSERVATIONS Drinkability WaterWell Volunteer where
observe (Drinkability waterWell) volunteer =
volunteer { vReport = drinkability } where
drinkability = undrinkableIf $ honesty volunteer `xor`
odorQuale (perceive (Odor waterWell) volunteer) &&
clarityQuale (perceive (Clarity waterWell) volunteer) &&
fullnessQuale (perceive (Fullness waterWell) volunteer)
undrinkableIf True = "undrinkable"
undrinkableIf False = "drinkable"
xor False = id
xor True = not
infixr 2 xor
if I didn't make a mistake. It's maybe a little bit more readable.
Btw. How would I rewrite this in applicative style? I came up with something like
drinkability = undrinkableIf $ xor . honesty
<*> ( (&&) . odorQuale . (perceive $ Odor waterWell)
<*> (&&) . clarityQuale . (perceive $ Clarity waterWell)
<*> fullnessQuale . (perceive $ Fullness waterWell) ) $ volunteer
(not sure it would still compile). However, seems not that readable anymore. Is it not recommended to use applicative style with binary infix operators? Or is there a way to make them readable again?
Regards,
Bastian
More information about the Beginners
mailing list