简体   繁体   English

Haskell(a-> a-> Bool)函数定义

[英]Haskell (a -> a -> Bool) function definition

Hi im new to haskell and im having hard time with function definition. 嗨,我是haskell的新手,并且对函数定义不满意。 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. 第二个参数应该是一个接受a -> a-> Bool (例如,大于)的函数。 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) . insort的第二个参数的类型为(a -> a -> Bool) insort (a -> a -> Bool) insort (a -> a -> Bool) This is the type of functions that take two a 's and return a Bool . 这是采用两个a并返回Bool的函数的类型。 Here a is Int . 这里aInt

The expression 4 > 3 (which is just syntactic sugar for (>) 4 3 ) is just of type Bool . 表达式4 > 3 (仅是(>) 4 3语法糖)就是Bool类型。 Poor thing. 可怜的东西。

You needed to pass the (>) function to insort . 您需要将(>)函数传递给insort

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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