haskell 中的 Concat 函数

Concat functions in haskell

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

Make a function "func" that receives a function " f " (String -> String) type and a String "s" that returns the reverse of "s" concat with " f " application in "s"

我设法使“f”函数像这样:

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

console: f "HASKELL" -> "LLEKSAHHASKELL"

但是当涉及到 "func" 功能时,我不知道该做什么或在控制台中输入什么。下面这段代码在 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"

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

感谢您的帮助。

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

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

您还错误地使用了该函数,因为第一个参数不是字符串,而是将字符串映射到字符串的函数。例如,我们可以使用 id :: a -> a to return s, or reverse :: [a] -> [a] 来反转字符串:

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