Haskell函数用于查找字母数字 [英] Haskell function for finding letter number
本文介绍了Haskell函数用于查找字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案您要查找的两个函数位于
Data.List
中,它们是 elemIndex
和 elemIndices
。 elemIndices :: Eq a => a - > [a] - > [Int]
elemIndex :: Eq a => a - > [a] - >也许Int
λ> elemIndex'F'FROG
只需0
λ> elemIndices'E'TEETER
[1,2,4]
寻找像这样的东西,写下你想要的类型通常是有帮助的,然后(如果可能的话)推广它。您可以通过这种方式经常在标准库中找到东西。
Does Haskell have a function that lets you find the number of a particular element of a word i.e. [char] so the element number of 'F' in "FROG" is 0. Also, what about words where that letter appears more than once, can this function return a list of letter positions i.e. 'E' in "TEETER" would be [1,2,4]?
解决方案
The two functions you're looking for are in Data.List
, they are elemIndex
and elemIndices
.
elemIndices :: Eq a => a -> [a] -> [Int]
elemIndex :: Eq a => a -> [a] -> Maybe Int
λ> elemIndex 'F' "FROG"
Just 0
λ> elemIndices 'E' "TEETER"
[1,2,4]
When looking for something like this it's often helpful to write down the type you want and then (if possible) generalize it. You can often find things in the standard library this way.
这篇关于Haskell函数用于查找字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文