带映射的 Haskell 高阶函数

发布于 2024-11-15 03:22:09 字数 579 浏览 6 评论 0原文

test::Int->(Int-> Char)->Char
test n f =  f(n)

DD::Int->Char
DD a | a==1 = '1'

test 是一个当前返回 char 值的高阶函数,我需要返回一个 String 作为 test::Int->(Int-> Char )->String

我将函数体更改为

test::Int->(Int-> Char)->String
test n f =  map f(n)

Error

Type error in application
*** Expression     : map f n
*** Term           : n
*** Type           : Int
*** Does not match : [a]

我如何将此函数应用于带有 map 的字符串?我哪里错了?

test::Int->(Int-> Char)->Char
test n f =  f(n)

DD::Int->Char
DD a | a==1 = '1'

test which is a higher order function currently returning a char value , i required to return a String as test::Int->(Int-> Char)->String

i changed to function body as

test::Int->(Int-> Char)->String
test n f =  map f(n)

Error

Type error in application
*** Expression     : map f n
*** Term           : n
*** Type           : Int
*** Does not match : [a]

How can i apply this function to a string with map ? where i went wrong ?

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

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

发布评论

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

评论(1

枕梦 2024-11-22 03:22:09

由于字符串只是一个字符列表,因此尝试返回一个字符列表:

test n f =  [f n]

顺便说一句,在 Haskell 中,如果不是真正需要的话,我们通常不使用括号。

Since a string is simply a list of chars, try out to return a list of chars:

test n f =  [f n]

BTW, in Haskell we usually don't use paranthesis if they are not really needed.

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