大家好~我对于Haskell 还是个新手,虚心求教, 我再做一个关于haskell 的列表匹配问题,
加入我有两个List
list1: [String]
list2: [String]
list1 = ["A2","B3","A1",“A4”]
list2 = ["C1","A3",“A4”,"A6"]
我想计算每个元素不同的地方,
第一步是先算完全相同的部分然后返回相同的个数 Int 和一个去掉这个元素的列表
第一步代码如下:
calSame :: [String] -> [String] -> (Int,[String])
calSame [] x = (0, x)
calSame x [] = (0, [])
calSame (x:xs) (y:ys)
| x == y = increment (calSame xs ys)
| otherwise = append y (calSame xs ys)
where
increment (count, results) = (count + 1, results)
append y (count, results) = (count, y:results)
返回值为 (1,["A1","A3","A6"])
第二步为在这个列表中计算和第一个列表部分相同的数字, 分别为字母部分和数字部分,
也就是比较去除A4 后部分相同的数字, 也就是说
["A2","B3","A1"] 和 ["A1","A3","A6"]作比较
期待返回值为 (2,2)
因为两个列表中 A 有 2 个是一样的, 1 和 3 也是共有的
代码如下
这是计算字母相同的代码
calHead:: [String] -> [String] -> (Int,[String])
calHead [] x = (0, x)
calHead x [] = (0, [])
calHead (x:xs)(y:ys)
| compare x y == True = increment (calHead xs ys)
| otherwise =append y (calNote [x] ys)
where
increment (count, results) = (count + 1,results)
append y (count, results) = (count, y:results)
compare a b
| head a == head b = True
| otherwise = False
但是这里返回的值不是正确的,返回的值为 (1,["C1","A4","A6"])
数字因该是2 才对啊~我哪里出了问题??我觉得应该在返回值里继续查找相同,可是我想破头了也没想出来~~求教!!
还有有没有办法把计算首和尾合并起来计算?就像
calComb :: [String] ->[String]->(Int,Int)
然后把后两部计算合并起来?
还有~有没办法把三步都合并起来组合返回三个数字?比如(1,2,2)
公式应该是这样?
bigComb :: [String]->[String]->(Int,Int,Int)
虚心求教!!!感谢各位大神!