haskell - 在 Haskell 中对字符串列表中的字符串进行编号
问题描述
所以我必须对字符串列表中的每个字符串进行编号
像这样:
numberLines ["one", "two", "three"]
==> ["1: one", "2: two", "3: three"]
我试过这个:
numberLines = map (\g -> (x + 1) : g) where
x = 0
但当然它没有用
解决方案
在 Haskell 中,变量是不可变的,所以你不能像 Python 这样的命令式语言那样改变变量的值。
例如,您可以使用将字符串列表与szipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
列表一起压缩:Int
numberLines :: [String] -> [String]
numberLines = zipWith f [1 :: Int .. ]
where f i s = show i ++ ' ' : ':' : s
因此,此处f
采用 aInt
和 aString
并生成一个字符串,其中包含数字的字符串表示形式和字符串。"i: s"
i
s
推荐阅读
- go-templates - 如何压缩两个数组
- c# - 方法调用后全局声明的变量返回 Null
- python - 使用 python 抓取网页时出错
- javascript - 如何在 React js 中访问登录的谷歌用户?
- reactjs - 如何修复 reactjs 语法错误:意外令牌?
- python - 如何使用 fit_generator 解决数据不平衡的问题
- ruby-on-rails - Rails rake 任务在不使用密件抄送的情况下循环多个电子邮件收件人
- php - 如何使用 Artax 进行发布请求
- xpath - Xpath 返回标签位置(包含文本)在集合中
- python - 如何检查列表中是否存在元素?