首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell求qwerty距离的2d指标

Haskell求qwerty距离的2d指标
EN

Stack Overflow用户
提问于 2013-09-20 11:55:02
回答 2查看 177关注 0票数 0

在haskell中实现qwerty距离函数。作为其中的一部分,我需要一个函数,它将返回定义结构(向量、列表、数组?)中特定元素的i,j索引。我被困住了。

代码语言:javascript
复制
import qualified Data.Vector as V
qwerty = V.fromList $ map V.fromList
        [ "qwertyuiop", "asdfghjkl;'", "zxcvbnm,./" ]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-20 12:25:26

这是一项将索引与列表元素关联的任务。通常,使用zip [0..] xs很容易做到这一点。因此,首先将索引与每个字符串关联,然后将索引与字符串中的每个字符关联。

代码语言:javascript
复制
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查找的线性成本:

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2013-09-20 12:18:06

你可以尝试这样的方法:

代码语言:javascript
复制
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 = Nothing
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18916171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档