繁体   English   中英

haskell 中的连接函数

[英]Concat functions in haskell

我是 Haskell 的新手,所以我有很多问题需要您的帮助。

制作一个 function “func”,它接收一个 function “f”(字符串 -> 字符串)类型和一个字符串“s”,它返回“s”与“s”中的“f”应用程序的反向连接

我设法使“ f ” function 像这样:

f:: String -> String
f x -> reverse(x) ++ x

控制台:f“HASKELL”->“LLEKSAHHASKELL”

但是当涉及到“func” function 时,我不知道该怎么做或在控制台中输入。 下面的代码在 GHC 中工作,但我不知道为什么,它不接受条目。

func:: (String -> String) -> String -> String
func f s = (reverse(s) ++ f "")

console: func "HASKELL" "ROCKS"

<interactive>:49:6: error:
    • Couldn't match expected type ‘String -> String’
                  with actual type ‘[Char]’
    • In the first argument of ‘func’, namely ‘"HASKELL"’
      In the expression: func "HASKELL" "ROCKS"
      In an equation for ‘it’: it = func "HASKELL" "ROCKS"

如果有更好的方法可以告诉我吗?

谢谢你的帮助。

您的 function 几乎是正确的,除了您没有使用s作为 function 应用程序的参数s

func:: (String -> String) -> String -> String
func f s = reverse s ++ f s

此外,您还以错误的方式使用 function,因为第一个参数不是字符串,而是将字符串映射到字符串的 function。 例如,我们可以使用id:: a -> a来返回s ,或者使用reverse:: [a] -> [a]来反转字符串:

Prelude> func id "haskell"
"lleksahhaskell"
Prelude> func reverse "haskell"
"lleksahlleksah"

暂无
暂无

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

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