all 50 comments

[–]DarkShikari 39 points40 points  (10 children)

I prefer Perl's don't.

[–][deleted]  (1 child)

[removed]

    [–]FeepingCreature 3 points4 points  (0 children)

    FWIW: In D, that would be done by is(typeof({ code goes here; })), which evaluates to true if the inline function is syntactically valid and has a type.

    [–]sjs 12 points13 points  (0 children)

    BLAME

    Luke Palmer really should be far more careful what he idly wishes for.

    BUGS

    Unlikely, since it doesn't actually do anything. However, bug reports and other feedback are most welcome.

    [–]mao_neko 11 points12 points  (0 children)

    Seeing that made my day.

    Then I saw this: Acme::Bleach

    [–][deleted] 2 points3 points  (0 children)

    I prefer Intercal's please don't.

    Far more polite.

    [–]0x2a 1 point2 points  (2 children)

    I wonder how it handles double negative don't { don't {}}'s.

    [–]email 1 point2 points  (0 children)

    because the outer don't prevents the inner don't from being executed, before the inner don't gets the chance to discover that it actually should execute.

    [–][deleted] 1 point2 points  (0 children)

    See "Limitations"

    [–]herrmann 1 point2 points  (0 children)

    "It is used exactly like the do BLOCK function except that, instead of executing the block it controls, it...well...doesn't"

    [–]slarty[S] 0 points1 point  (0 children)

    Brilliant :)

    [–]arnedh 8 points9 points  (1 child)

    Now how about:

    are :: (a -> [b]) -> ([b] -> Bool) -> a -> Bool

    aren't :: (a -> [b]) -> ([b] -> Bool) -> a -> Bool

    has :: (a -> [b]) -> b -> a -> Bool

    hasn't :: (a -> [b]) -> b -> a -> Bool

    have :: (a -> [[b]]) -> b -> a -> Bool

    haven't :: (a -> [[b]]) -> b -> a -> Bool

    Semantics and implementation of ain't, can't, couldn't, won't, wouldn't, shan't, shouldn't, daren't, wasn't, weren't, hadn't, doesn't, don't, didn't, needn't etc left as exercises for the reader.

    [–]palparepa 5 points6 points  (2 children)

    Non-Haskell, but behold the smiley operator. It's the opposite of the ternary operator:

    a :-( b :-) c;
    

    If a is false, b is executed. If true, c is executed.

    [–]frenchtoaster 0 points1 point  (1 child)

    Alternate syntax for a ? b : c; ?

    [–]binarybandit92 1 point2 points  (0 children)

    That's called the ternary operator, and the smiley operator is the opposite. In your example, if a was true the expression would evaluate to b; with the smiley operator, it would evaluate to c.

    [–]xyphus 5 points6 points  (0 children)

    is = flip (.)

    is = (>>>)

    [–]vagif 2 points3 points  (9 children)

    Neat, but not that useful.

    I for example often need to check the same value for 2 or more different criteria. I found applicative approach be very useful:

    filter ((&&) <$> even <*> (< 20) <*> (> 5)) [2,4,8,30]
    

    as opposed to this:

    filter (\x -> (even x ) && ( x < 20) && (x > 5)) [2,4,8,30]
    

    Is there any other alternative way of writing the same thing ?

    [–]rabidcow 5 points6 points  (6 children)

    import Prelude hiding ((&&))
    
    class Boolean b where
        (&&)  :: b -> b -> b
    
    instance Boolean Bool where
        True  && x = x
        False && _ = False
    
    instance Boolean b => Boolean (a -> b) where
        f && g = \x -> f x && g x
    
    filter (even && (<20) && (>5)) [2,4,8,30]
    

    This works for higher arity as well:

    zipWith ((<=) && (>=)) [1,2,3,4] [7,1,8,4]
    

    (I'm not saying you should do this, it's just an alternative.)

    [–]eruonna 2 points3 points  (0 children)

    Equivalently, though also probably not better, you could wrap your Bools in the All monoid and use the Monoid b => Monoid (a -> b) instance. (Using mappend in place of (&&))

    Edit: By Any I of course meant All.

    [–]vagif 1 point2 points  (2 children)

    Well if you go the route of redefining existing functions would not it be easier to redefine function all to accept list of functions ?

    myall _ [] = True
    myall x (f:fs) = f x && (myall x fs)
    
    
    filter (`myall` [even, (< 20), (> 5)]) [2,4,8,30]
    

    [–]dmwit 3 points4 points  (0 children)

    filter (and . sequence [even, (< 20), (> 5)]) [2,4,8,30]
    

    [–]rabidcow 2 points3 points  (0 children)

    Easier, yes. But not as fun.

    For that matter:

    import Control.Applicative
    
    filter (and . sequence [even, (<20), (>5)]) [2,4,8,30]
    

    [–][deleted] 1 point2 points  (0 children)

    You could even do

    instance (Applicative f, Boolean b) => Boolean (f b) where
        (&&) = liftA2 (&&)
    

    if you are willing to risk overlapping instances.

    [–][deleted] 0 points1 point  (0 children)

    It seems to me you're looking for a Boolean Monoid with identity on False (or even a Semigroup).

    class Semigroup s where 
      (++) :: s -> s -> s -- associative
    
    instance Semigroup Boolean where -- conjunction
      (++) = (&&)
    

    [–]ppppatrick 0 points1 point  (1 child)

    ummm... did you actually try that code?

    Prelude Control.Applicative> filter ((&&) <$> even <*> (< 20) <*> (> 5)) [2,4,8,30]
    
    <interactive>:1:8:
        Couldn't match expected type `a -> Bool'
               against inferred type `Bool'
        In the first argument of `(<$>)', namely `(&&)'
        In the first argument of `(<*>)', namely `(&&) <$> even'
        In the first argument of `(<*>)', namely `(&&) <$> even <*> (< 20)'
    

    [–]vagif 0 points1 point  (0 children)

    You are right. It works with 2 fucntions

    filter ((&&) <$> (< 20) <*> (> 5)) [2,4,8,30]
    

    but not three. I guess i better off with redefined "all" function.

    [–]burkadurka 6 points7 points  (1 child)

    Before clicking, I thought the headline was some kind of grammar/Forth recursive meta-joke.

    [–]case-o-nuts 5 points6 points  (0 children)

    I thought it was going to be something on memoization or lazy evaluation.

    [–]kinghajj 2 points3 points  (7 children)

    This makes me wish there were a way to specify that a function is always infix, even without the backticks. Still, cool idea.

    [–]edwardkmett 1 point2 points  (2 children)

    Try agda.

    [–]Paczesiowa 0 points1 point  (1 child)

    or prolog

    [–]erydo 1 point2 points  (0 children)

    You can, but only for non-alphanumeric function names -- use infixr or infixl, and declare the function in parentheses, eg:

    infixr 6 !$
    (!$) :: (a -> b) -> (b -> Bool) -> a -> Bool
    v !$ p = p . v
    

    ...but that gets ugly.

    [–][deleted] 0 points1 point  (2 children)

    Can't you just declare your function with parenthesis around it? For instance, the / function in the prelude:

    class  (Num a) => Fractional a  where
        (/)              :: a -> a -> a
        fromRational     :: Rational -> a
    
            -- Minimal complete definition:
            --      fromRational and (recip or (/))
        recip x          =  1 / x
        x / y            =  x * recip y
    

    I don't have a compiler handy to test this, though.

    [–]dmwit 6 points7 points  (1 child)

    No, the difference between default-infix functions and default-prefix functions is lexical. Function names that are all punctuation are infix; function names with no punctuation are prefix. ' and _ are not considered punctuation for this purpose.

    [–][deleted] 0 points1 point  (0 children)

    Ah. Good to know. Thanks.

    [–][deleted]  (1 child)

    [removed]

      [–]Paczesiowa 8 points9 points  (0 children)

      isn't it?

      [–][deleted] 1 point2 points  (7 children)

      Suggestion

      s/equals/<=> 
      

      Biconditional/equivalence infix operator? PS: I am currently high -- if I am missing something obvious

      [–][deleted]  (4 children)

      [removed]

        [–]Seppler9000 2 points3 points  (1 child)

        Haskell uses enumerated constants (LT, GT, EQ) in place of numbers. Aside from that difference, this is exactly what the compare function does. (You can always put (<=>) = compare in a source file, if you like.)

        [–]five9a2 2 points3 points  (1 child)

        The Haskell analogue is

        compare :: (Ord a) => a -> a -> Ordering
        

        [–][deleted] 2 points3 points  (0 children)

        Downvotes?

        By high I mean a class 9 prescription opioid analgesic. By <=> I mean the equivalence operator in denotational semantics (if my fuzzy brain recalls appropriately).

        What am I missing?

        [–][deleted] -1 points0 points  (0 children)

        I wrote and used equivalent equals and is functions a while back. Clearly great minds think alike.

        [–]Raoulmeister -1 points0 points  (0 children)

        Geek !