简体   繁体   中英

Haskell (a -> a -> Bool) function definition

Hi im new to haskell and im having hard time with function definition. In a assignment i need to use this function

insort :: [a] -> (a -> a -> Bool) -> [a]
insort [] _     = []
insort (x : xs) f   =   ins x (insort xs f)
                        where
                        ins x []    = [x]
                        ins x (y : ys) =    if (f x y) 
                                            then x : y : ys
                                            else y : ins x ys

but i can't figuring out how to use it.. for me is seems like i should be:

insort [1,2,3,5,6] (4 > 3)

and thanks you for your help!

The second argument should be a function that accepts a -> a-> Bool , say, greater than. This function will be called each element in the list.

You should use it like:

insort [1, 2, 3, 4, 5, 6] (>)

The second argument to insort has type (a -> a -> Bool) . This is the type of functions that take two a 's and return a Bool . Here a is Int .

The expression 4 > 3 (which is just syntactic sugar for (>) 4 3 ) is just of type Bool . Poor thing.

You needed to pass the (>) function to insort .

insort [1, 2, 3, 4, 5, 6] (>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM