功能期望char代替[char]

发布于 2025-01-23 03:18:16 字数 842 浏览 0 评论 0原文

我是Haskell和功能编程的新手,无法理解为什么此功能无法识别正确的类型:

mformat :: [Char] -> [Char] -> [Char]
mformat first last = ((formatted first last) ++ " " ++ first ++ " " ++ last ++ ".")
    where formatted (f:_) (l:_) = (f ++ "." ++ l ++ ".")

这会导致错误:

teststuff.hs:42:40: error:
    * Couldn't match type `Char' with `[Char]'
      Expected: [[Char]]
        Actual: [Char]
    * In the second argument of `formatted', namely `last'
      In the first argument of `(++)', namely `(formatted first last)'
      In the expression:
        (formatted first last) ++ " " ++ first ++ " " ++ last ++ "."
   |
42 | mformat first last = ((formatted first last) ++ " " ++ first ++ " " ++ last ++ ".")
   |                                        ^^^^
Failed, no modules loaded.

我不明白这里有什么问题,任何帮助都将不胜感激。

I am new to Haskell and functional programming, and can't understand why this function cannot identify the correct type:

mformat :: [Char] -> [Char] -> [Char]
mformat first last = ((formatted first last) ++ " " ++ first ++ " " ++ last ++ ".")
    where formatted (f:_) (l:_) = (f ++ "." ++ l ++ ".")

which causes the error:

teststuff.hs:42:40: error:
    * Couldn't match type `Char' with `[Char]'
      Expected: [[Char]]
        Actual: [Char]
    * In the second argument of `formatted', namely `last'
      In the first argument of `(++)', namely `(formatted first last)'
      In the expression:
        (formatted first last) ++ " " ++ first ++ " " ++ last ++ "."
   |
42 | mformat first last = ((formatted first last) ++ " " ++ first ++ " " ++ last ++ ".")
   |                                        ^^^^
Failed, no modules loaded.

I don't understand what is wrong here, any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2025-01-30 03:18:16

问题在您的格式函数中。您是在字符串上匹配的模式,您获得char s(f& l),然后尝试尝试将它们与字符串串联。您不能将char字符串[char])相连。

mformat :: String -> String -> String
mformat first last = ((formatted first last) ++ " " ++ first ++ " " ++ last ++ ".")
    where
      formatted :: String -> String -> String 
      formatted (f:_) (l:_) = ([f] ++ "." ++ [l] ++ ".")

-- ...
      formatted (f:_) (l:_) = (f : "." ++ l : ".")

类型的检查器认为,在您的情况下,fl必须是列表 - 因为您正在尝试将它们串联。然后,它从列表构造函数中输入(通过模式匹配),first最后strings ie ie [string]的列表/code>或[[char]]

The issue is in your formatted function. You are pattern matching on a String, you get Chars (f & l) and then you try concatenating them with a String. You cannot concatenate a Char with a String ([Char]).

mformat :: String -> String -> String
mformat first last = ((formatted first last) ++ " " ++ first ++ " " ++ last ++ ".")
    where
      formatted :: String -> String -> String 
      formatted (f:_) (l:_) = ([f] ++ "." ++ [l] ++ ".")

or

-- ...
      formatted (f:_) (l:_) = (f : "." ++ l : ".")

The type checker thinks thinks that f and l in your case must be lists - because you are attempting to concatenate them. Then it infers (via pattern matching) from the list constructor, that first and last are lists of Strings i.e. [String] or [[Char]].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文