如果标题不好,请见谅...
所以我的问题是我得到了这个代码:
asd (a:as) (b:bs)= tail(rox(tail(rox (tail (rox (a:as) (b:bs))) (b:bs))) (b:bs))
rox [] as = as
rox as [] = as
rox (a:as) (b:bs) = map abs (a-b:rox as bs)
我的类型必须是:asd :: [Int]->[Int]->[Int]
我花了半天时间想出一个好的解决方案,但我就是想不出一个,所以如果有人能帮助我,我会很高兴的。我想从(a:as)列表中减去(b:bs)列表,然后我想删除第一个数字并使用结果(结果-(b:bs))再次执行,直到我得到一个(长度(b:bs))-1)列表。如果(a:as)/结果低于(b:bs),例如:00101<10011 我需要将所有(b:bs)数字更改为0(如果结果再次更高,请继续使用(b:bs))。该示例在上面的代码中运行良好,但我想将它与任何列表一起使用。也许我需要使用迭代功能,但我无法弄清楚如何。
这是一个例子:
11010(a:as)
101(b:bs)
01110(result)
1110(after using tail)
101(b:bs again)
0100(result)
100(after tail)
101(b:bs)
001(result)
01(final result after using tail, so its 1number shorter than the (b:bs) list
非常感谢你的帮助!
编辑:有了这个我可以检查哪个二进制数更高,所以我可以将所有 bs 数变为 0,但我不知道如何实现它。
(%>=%) :: [Int] -> [Int] -> Bool
(%>=%) [] [] = True
(%>=%) as [] = True
(%>=%) [] bs = False
(%>=%) as bs
| filter (/=0) (takeWhile (>(-1))(ro as bs))==[]=False
| elem 1 (takeWhile (>(-1))(ro as bs))==True=True
ro :: [Int] -> [Int] -> [Int]
ro [] bs = bs
ro as [] = as
ro (a:as) (b:bs) = (2*a) - b: ro as bs
结果:
asd [1,1,1,0,0,1,0,1,0,0,0,0] [1,1,0,1,1]
asd [0,1,1,1,1,0,1,0,0,0,0] [1,1,0,1,1]
asd [1,1,1,1,0,1,0,0,0,0] [1,1,0,1,1]
asd [0,1,0,1,1,0,0,0,0] [1,1,0,1,1]
asd [1,0,1,1,0,0,0,0] [1,1,0,1,1]
asd [0,1,1,0,0,0,0] [1,1,0,1,1] < here is something wrong because its: 1,1,0,1,0,0,0 and from here everything is wrong
asd [1,1,0,0,0,0] [1,1,0,1,1]
asd [1,0,0,0,0] [1,1,0,1,1]
asd [0,0,0,0] [1,1,0,1,1]
[0,0,0,0]