Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 我应该如何使用函数组合在Haskell中定义或编写我的函数?_Function_Haskell_Composition_Pointfree - Fatal编程技术网

Function 我应该如何使用函数组合在Haskell中定义或编写我的函数?

Function 我应该如何使用函数组合在Haskell中定义或编写我的函数?,function,haskell,composition,pointfree,Function,Haskell,Composition,Pointfree,我在Haskell中定义了一个函数,它应该是平方,然后给给定列表中的所有数字加1。我想用函数组合来编写该函数,但不幸的是,它不适用于点函数,但当我用括号来编写函数时,它就起作用了。我不明白为什么它不适用于点,或者更确切地说,为什么它不适用于函数构图 square :: Int -> Int square x = x * x funSqr :: [Int] -> [Int] funSqr xs = [(square x) | x <- xs] funAdd1 :: [Int]

我在Haskell中定义了一个函数,它应该是平方,然后给给定列表中的所有数字加1。我想用函数组合来编写该函数,但不幸的是,它不适用于点函数,但当我用括号来编写函数时,它就起作用了。我不明白为什么它不适用于点,或者更确切地说,为什么它不适用于函数构图

square :: Int -> Int
square x = x * x

funSqr :: [Int] -> [Int]
funSqr xs = [(square x) | x <- xs]

funAdd1 :: [Int] -> [Int]
funAdd1 xs = [(x + 1) | x <- xs]

funFoldr :: [Int] -> Int 
funFoldr [] = 0
funFoldr (x:xs) = x + (funFoldr xs)

fun :: [Int] -> Int
fun xs = funFoldr(funAdd1(funSqr xs))
有人能照亮我的路吗

非常感谢
Asena

Haskell解析规则101:函数应用程序绑定比任何中缀运算符都更紧密。空白的数量无关紧要。所以

funFoldr.funAdd1.funSqr xs
   ≡ funFoldr . funAdd1 . funSqr xs
   ≡ funFoldr . funAdd1 . (funSqr xs)
现在,
funSqr-xs
不再是一个函数(只是将函数应用于
xs
的结果)。你不能合成非函数的东西

你想尝试的是这个,它确实有效:

(funFoldr.funAdd1.funSqr) xs
更常见的是,这是书面的

funFoldr . funAdd1 . funSqr $ xs

或者,如果您只是不提及
xs
,则可以避免
funSqr-xs
分组:

fun :: [Int] -> Int
fun = funFoldr . funAdd1 . funSqr

这就是所谓的无点风格。

试试
fun=funFoldr。funAdd1。funSqr
Try
fun=sum。映射(+1).join(*)
在导入控制.Monad(join)之后。
funFoldr . funAdd1 $ funSqr xs
fun :: [Int] -> Int
fun = funFoldr . funAdd1 . funSqr