Function result being inversed

Hello, I am confused by the following code. I would expect results of True, False. $ ghci *Main> let f x = x 4 *Main> f(<) 3 False *Main> f(<) 5 True This came about because I was trying to refactor a sort function I wrote: mySort [] = [] mySort (h:t) = (f (<= h)) ++ [h] ++ (f (> h)) where f x = mySort (filter x t) I came up with this, which appears to work, though the comparison operators are backwards. mySort [] = [] mySort (h:t) = f(>) ++ [h] ++ f(<=) where f x = mySort (filter (x h) t) Cheers, Xavier

f (<) 3 == (<) 4 3 == 4 < 3 == False
On 25 January 2011 21:00, Xavier Shay
Hello, I am confused by the following code. I would expect results of True, False.
$ ghci *Main> let f x = x 4 *Main> f(<) 3 False *Main> f(<) 5 True
This came about because I was trying to refactor a sort function I wrote:
mySort [] = [] mySort (h:t) = (f (<= h)) ++ [h] ++ (f (> h)) where f x = mySort (filter x t)
I came up with this, which appears to work, though the comparison operators are backwards.
mySort [] = [] mySort (h:t) = f(>) ++ [h] ++ f(<=) where f x = mySort (filter (x h) t)
Cheers, Xavier
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tuesday 25 January 2011 21:00:11, Xavier Shay wrote:
Hello, I am confused by the following code. I would expect results of True, False.
$ ghci *Main> let f x = x 4 *Main> f(<) 3 False *Main> f(<) 5 True
It's because f (<) k = (f (<)) k = ((<) 4) k = (<) 4 k = 4 < k or, shorter, (<) 4 = (4 <)
This came about because I was trying to refactor a sort function I wrote:
mySort [] = [] mySort (h:t) = (f (<= h)) ++ [h] ++ (f (> h)) where f x = mySort (filter x t)
I came up with this, which appears to work, though the comparison operators are backwards.
mySort [] = [] mySort (h:t) = f(>) ++ [h] ++ f(<=) where f x = mySort (filter (x h) t)
A right operator section, (<*> x) translates to flip (<*>) x in prefix notation.
Cheers, Xavier

You want something like:
f x = x 4
main = do
print $ f $ (<) 3
print $ f $ (<) 5
=> True
False
(< 3) 4 translates to (4 < 3)
-deech
On Tue, Jan 25, 2011 at 2:00 PM, Xavier Shay
Hello, I am confused by the following code. I would expect results of True, False.
$ ghci *Main> let f x = x 4 *Main> f(<) 3 False *Main> f(<) 5 True
This came about because I was trying to refactor a sort function I wrote:
mySort [] = [] mySort (h:t) = (f (<= h)) ++ [h] ++ (f (> h)) where f x = mySort (filter x t)
I came up with this, which appears to work, though the comparison operators are backwards.
mySort [] = [] mySort (h:t) = f(>) ++ [h] ++ f(<=) where f x = mySort (filter (x h) t)
Cheers, Xavier
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thanks to all responses. Makes sense now. For the record I have ended up with this: mySort [] = [] mySort (h:t) = f(<=) ++ [h] ++ f(>) where f x = mySort (filter (not . x h) t) I have a feeling I may be able to make the following work with some sort of type declaration but I haven't really learned much about them yet so I will revisit later. (At the moment it does not compile.) mySort [] = [] mySort (h:t) = f(<=) ++ [h] ++ f(>) where f x = mySort (filter (h x) t) Cheers, Xavier On 26/01/11 7:00 AM, Xavier Shay wrote:
Hello, I am confused by the following code. I would expect results of True, False.
$ ghci *Main> let f x = x 4 *Main> f(<) 3 False *Main> f(<) 5 True
This came about because I was trying to refactor a sort function I wrote:
mySort [] = [] mySort (h:t) = (f (<= h)) ++ [h] ++ (f (> h)) where f x = mySort (filter x t)
I came up with this, which appears to work, though the comparison operators are backwards.
mySort [] = [] mySort (h:t) = f(>) ++ [h] ++ f(<=) where f x = mySort (filter (x h) t)
Cheers, Xavier
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Tuesday 25 January 2011 21:15:04, Xavier Shay wrote:
Thanks to all responses. Makes sense now. For the record I have ended up with this:
mySort [] = [] mySort (h:t) = f(<=) ++ [h] ++ f(>) where f x = mySort (filter (not . x h) t)
I have a feeling I may be able to make the following work with some sort of type declaration but I haven't really learned much about them yet so I will revisit later. (At the moment it does not compile.)
mySort [] = [] mySort (h:t) = f(<=) ++ [h] ++ f(>) where f x = mySort (filter (h x) t)
Well, h is not a function, so (h x) isn't well formed. You want a left operator section there, so you have to make the function x infix: where f x = mySort (filter (h `x`) t) Backticks turn ordinary prefix functions into infix functions, a `mod` b === mod a b
Cheers, Xavier
participants (4)
-
aditya siram
-
Daniel Fischer
-
Tobias Brandt
-
Xavier Shay