
outStanza | (isMessage) = outMessage | (isPresence) = outPresence | (isIQ) = outIQ Why such a style doesn't work, so I must write ugly code like that: outStanza a | (isMessage a) = outMessage a | (isPresence a) = outPresence a | (isIQ a) = outIQ a so, guards can't be useful in point-free function definitions in any way

Hi
Why such a style doesn't work, so I must write ugly code like that:
outStanza a | (isMessage a) = outMessage a | (isPresence a) = outPresence a | (isIQ a) = outIQ a
You can make it slightly prettier, since the brackets are not necessary: outStanza a | isMessage a = outMessage a | isPresence a = outPresence a | isIQ a = outIQ a Although I suspect that outMessage crashes if isMessage returns False? And perhaps outMessage is written as outMessage (Message a b) = ... In which case, I'd write: outStanza (Message a b) = ... And then the code no longer looks ugly, uses pattern matching nicely, requires no "is..." functions and won't crash if things like outMessage are called incorrectly. [Note, everything beyond the no need for brackets is a bit of a guess, just possible ideas to think about] Thanks Neil

On Tue, Jul 22, 2008 at 10:27 AM, Neil Mitchell
Hi
Why such a style doesn't work, so I must write ugly code like that:
outStanza a | (isMessage a) = outMessage a | (isPresence a) = outPresence a | (isIQ a) = outIQ a
You can make it slightly prettier, since the brackets are not necessary:
outStanza a | isMessage a = outMessage a | isPresence a = outPresence a | isIQ a = outIQ a
Also, if it really is in that format, maybe you can write something like: switch v pairs = maybe (error "no match") ($v) (lookupWith ($v) pairs) And then you can write the list point-free, though you don't get the nice guard syntax. I guess lookupWith must be one of my local functions, but it's easy to write too.

On Jul 22, 2008, at 13:18 , L29Ah wrote:
outStanza | (isMessage) = outMessage | (isPresence) = outPresence | (isIQ) = outIQ
Why such a style doesn't work, so I must write ugly code like that:
Because the Haskell 98 Report specifies that guards are rewritten in a specific way, which in your case produces invalid code. See http://haskell.org/onlinereport/decls.html#pattern-bindings for details. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Jul 22, 2008, at 6:32 PM, Brandon S. Allbery KF8NH wrote:
On Jul 22, 2008, at 13:18 , L29Ah wrote:
outStanza | (isMessage) = outMessage | (isPresence) = outPresence | (isIQ) = outIQ
Why such a style doesn't work, so I must write ugly code like that:
Because the Haskell 98 Report specifies that guards are rewritten in a specific way, which in your case produces invalid code. See http://haskell.org/onlinereport/decls.html#pattern-bindings for details.
You may mimic the syntax you wish by writing your own PF combinators, eg. by declaring outStanza = (isMessage .= outMessage .| (isPresence .= outPresence .| (isIQ .= outIQ .| ... ))) once you've defined combinators (p .= f) x = if p x then (f .Left) x else (f. Right) x and (f .| g) (Left a) = f a (f .| g) (Right b) = g b with appropriate infix priorities ( .| higher than .= ). But you still need the extra parentheses... jno
____________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

2008/7/22 J.N. Oliveira
But you still need the extra parentheses...
Not so! infixl 0 .| infixl 0 .|... -- 'otherwise' construct infix 1 .= (.=) :: (a -> Bool) -> (a -> b) -> (a -> Maybe b) (.|) :: (a -> Maybe b) -> (a -> Maybe b) -> (a -> Maybe b) (.|...) :: (a -> Maybe b) -> (a -> b) -> (a -> b) -- implementations left as exercise for the reader outStanza = isMessage .= outMessage .| isPresence .= outPresence .| isIQ .= outIQ .|... const 42 Hooray for abusing operators! Luke

outStanza | (isMessage) = outMessage | (isPresence) = outPresence | (isIQ) = outIQ
Why such a style doesn't work, so I must write ugly code like that:
outStanza a | (isMessage a) = outMessage a | (isPresence a) = outPresence a | (isIQ a) = outIQ a
so, guards can't be useful in point-free function definitions in any way
You just have to avoid all those pointless language constructs that let mere Haskellers deal with points, and define your own: import Control.Monad import Data.Maybe import Control.Arrow((&&&)) g |= rhs = uncurry (>>) . ((guard . g) &&& (return . rhs)) a +++ b = uncurry mplus . (a &&& b) (=|) = (fromJust .) outStanza = (=|) (((=="m") |= ("message: "++)) +++ ((=="p") |= ("presence: "++)) +++ ((=="i") |= ("iq: "++)) ) Sorry about mentioning those Strings, but then the names shouldn't really mention the points (Stanza/Message/..), either, right? Or have I missed the point of this exercise?-) Claus

On Tue, 22 Jul 2008, L29Ah wrote:
outStanza | (isMessage) = outMessage | (isPresence) = outPresence | (isIQ) = outIQ
Why such a style doesn't work, so I must write ugly code like that:
outStanza a | (isMessage a) = outMessage a | (isPresence a) = outPresence a | (isIQ a) = outIQ a
so, guards can't be useful in point-free function definitions in any way
It's sad that syntactic sugar makes people want even more syntactic sugar (some people thus call it syntactic heroin). You can easily achieve the wanted effect by a function like 'select' http://www.haskell.org/haskellwiki/Case and that way you can also avoid guards in many cases.
participants (8)
-
Brandon S. Allbery KF8NH
-
Claus Reinke
-
Evan Laforge
-
Henning Thielemann
-
J.N. Oliveira
-
L29Ah
-
Luke Palmer
-
Neil Mitchell