在haskell中实现qwerty距离函数。作为其中的一部分,我需要一个函数,它将返回定义结构(向量、列表、数组?)中特定元素的i,j索引。我被困住了。
import qualified Data.Vector as V
qwerty = V.fromList $ map V.fromList
[ "qwertyuiop", "asdfghjkl;'", "zxcvbnm,./" ]发布于 2013-09-20 12:25:26
这是一项将索引与列表元素关联的任务。通常,使用zip [0..] xs很容易做到这一点。因此,首先将索引与每个字符串关联,然后将索引与字符串中的每个字符关联。
import qualified Data.Map as M
qwmap = M.fromList $ concatMap f $ zip [0..] qwerty where
qwerty = ["qwertyuiop[]", "asddfghjkl;'#", "zxcvbnm,./"]
f (i,cs) = map (\(j,c) -> (c, (i,j))) $ zip [0..] cs或者,如果您不关心重复elemIndex查找的线性成本:
import Data.List
qp a = foldr f Nothing $ zip [0..] qwerty where
qwerty = ["qwertyuiop[]", "asddfghjkl;'#", "zxcvbnm,./"]
f (p,xs) n = maybe n (Just . (p,)) $ elemIndex a xs发布于 2013-09-20 12:18:06
你可以尝试这样的方法:
import qualified Data.Vector as V
qwerty = V.fromList $ map V.fromList
[ "qwertyuiop", "asdfghjkl;'", "zxcvbnm,./" ]
findElement :: Char -> Maybe (Int,Int)
findElement c = f $ V.findIndex (V.elem c) qwerty where
f (Just i) = (,) i `fmap` (V.elemIndex c $ qwerty V.! i)
f Nothing = Nothinghttps://stackoverflow.com/questions/18916171
复制相似问题