haskell中元素的索引

时间:2013-04-23 20:31:52

标签: list haskell

我有这两个列表[@,a,@,b,c,@,@,@,(de),@,@,@,f,g,@,h]["","","+","","","?","|","","","","","*","","","|",""],我想通过使用scanl or map函数来覆盖第二个列表,并在第一个列表中提取它前面的元素。这两个列表有不同类型的元素。

例如,我想如果第二个列表中的元素是/= "",那么在第一个列表中提取其后面的元素(例如,+获取a,{ {1}},? -> c *->(de))。

我试过这样的事情

|->g

其中map(\x-> if x /= "" then l2 !! (((elemIndices x l1)!!0)-1) else Epsilon)l1 l1 is ["","","+","","","?","|","","","","","*","","","|",""]但不起作用,l2 is [@,a,@,b,c,@,@,@,(de),@,@,@,f,g,@,h]也来自@ = Epsilon

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我想你想要像

这样的东西
combinedList = zip secondList (Epsilon : firstList)

然后查找“?”你只需要lookup "?" combinedList并获得Just (Literal 'c')

(显然你可以尝试抓住索引,但这里描述的情况似乎有点迂回。)