gpt4 book ai didi

Haskell 函数中的非详尽模式

转载 作者:行者123 更新时间:2023-12-03 14:56:13 25 4
gpt4 key购买 nike

以下代码编译得很好,但是当我尝试输入 helper 10 primes [] [] 它给了我:函数助手中的非详尽模式

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

eea :: Integer -> Integer -> (Integer, Integer, Integer)
eea 0 b = (0, 1, b)
eea a b = (y - b `div` a * x, x, g) where (x, y, g) = eea (b `rem` a) a

second :: (a, b, c) -> b
second (_, x, _) = x

chinese :: Integer -> [(Integer,Integer)] -> Integer
chinese n [] = 0
chinese n ((a,b):xs) = ((second(eea b (n `div`  b)) * (n `div`  b) )  * a) + chinese n (xs)

getN :: [(Integer,Integer)] -> Integer
getN [] = 1
getN ((x,y):xs) = y*(getN (xs))

chinese2 :: [(Integer,Integer)] -> Integer
chinese2 [] = 0
chinese2(x:xs) = chinese (getN (x:xs)) (x:xs)

chinese3 :: [(Integer,Integer)] -> Integer
chinese3 (x:xs) = if (chinese2(x:xs)) < 0 then chinese2 (x:xs) + (getN (x:xs)) else chinese2 (x:xs)

helper :: Integer -> [Integer] -> [Integer] -> [(Integer,Integer)] -> [Integer]
helper n [] (v) _ = []
helper n (x:y:xs) (v) (c:cs) = if (chinese3 (b:c:cs) == n) then (x:v) else helper n (xs) (x:v) ((n `mod` y,y):c:cs) where b =(n `mod` x,x)

最佳答案

chinese3没有空列表的情况。
helper缺少更多的情况,包括第二个参数有一个元素的情况。

关于Haskell 函数中的非详尽模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8749675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com