如何创建将计算单词中字母数的函数

时间:2018-04-13 09:44:32

标签: haskell

stringLength :: String -> Int
stringLength = undefined

这是上面的功能

1 个答案:

答案 0 :(得分:3)

String只是[Char]的别名,它是一个字符列表。因此length函数已经满足您的需求:

Prelude> length "hello"
5

您可以像这样编写自己的包装器:

stringLength :: String -> Int
stringLength s = length s

但在这种情况下,无点样式可能会更好:

stringLength :: String -> Int
stringLength = length

这提出了一个问题:为什么要有这样的功能呢?

相关问题