两个 Haskell 命令的问题,一个尝试查找并打印两个列表中的差异,另一个尝试输出最长的长度
Issues with two Haskell commands, one attempting to find and print differences in two lists, and one attempting to output the longest length
这是第一个,它试图获取两个列表并打印两者之间的所有差异。
EX.
> list_diff [1,6,1,3,4,3,5] [3,8,5,4,7,4,8]
[1,6,1,8,7,8]
list_diff :: Eq a => [a] -> [a] -> [a]
list_diff [] [] = []
list_diff x y = list_helper x y
list_helper [] [] = []
list_helper (x:xs) y
| x `elem` y = list_helper xs y
| otherwise = x:(list_helper xs y)
当我 运行 对此进行测试时(使用上面提供的示例),我收到以下输出:
[1,6,1*** 异常:HW1.hs:(12,1)-(15,39):函数 list_helper
中的非详尽模式
对于第二个函数,我尝试获取一系列值并确定哪个列表最长
EX.
progLanguages =
[ ("CptS121" , ["C"]),
("CptS122" , ["C++"]),
("CptS223" , ["C++"]),
("CptS233" , ["Java"]),
("CptS321" , ["C#"]),
("CptS322" , ["Python", "JavaScript"])]
INPUT: max_count progLanguages
OUTPUT: ("CptS322" , 2)
到目前为止我所拥有的根本不起作用,所以不用说我被卡住了。
max_count [] = error "bad"
max_count [x] = x
max_count (x:xs) = x max_helper (max_count xs)
where
max_helper (a,b) (a',b')
| length b > length b' = (a, length b)
| otherwise = (a', length b')
在此先感谢您提供的任何帮助。
您已定义 list_helper
:
list_helper [] [] = []
list_helper (x:xs) y
| x `elem` y = list_helper xs y
| otherwise = x:(list_helper xs y)
有关 non-exhaustive 模式匹配的警告意味着您的函数正在评估您尚未告诉它如何处理的参数。在这种情况下,第一个参数是一个空列表,但第二个参数 not 是空的。
你需要处理这个模式:
list_helper [] [] = []
list_helper [] y = y
list_helper (x:xs) y
| x `elem` y = list_helper xs y
| otherwise = x:(list_helper xs y)
Prelude> :{
Prelude| list_helper [] [] = []
Prelude| list_helper [] y = y
Prelude| list_helper (x:xs) y
Prelude| | x `elem` y = list_helper xs y
Prelude| | otherwise = x:(list_helper xs y)
Prelude| :}
Prelude> list_helper [1, 2, 3] [4, 5, 6]
[1,2,3,4,5,6]
这是第一个,它试图获取两个列表并打印两者之间的所有差异。
EX.
> list_diff [1,6,1,3,4,3,5] [3,8,5,4,7,4,8] [1,6,1,8,7,8]
list_diff :: Eq a => [a] -> [a] -> [a]
list_diff [] [] = []
list_diff x y = list_helper x y
list_helper [] [] = []
list_helper (x:xs) y
| x `elem` y = list_helper xs y
| otherwise = x:(list_helper xs y)
当我 运行 对此进行测试时(使用上面提供的示例),我收到以下输出:
[1,6,1*** 异常:HW1.hs:(12,1)-(15,39):函数 list_helper
中的非详尽模式对于第二个函数,我尝试获取一系列值并确定哪个列表最长
EX.
progLanguages =
[ ("CptS121" , ["C"]),
("CptS122" , ["C++"]),
("CptS223" , ["C++"]),
("CptS233" , ["Java"]),
("CptS321" , ["C#"]),
("CptS322" , ["Python", "JavaScript"])]
INPUT: max_count progLanguages
OUTPUT: ("CptS322" , 2)
到目前为止我所拥有的根本不起作用,所以不用说我被卡住了。
max_count [] = error "bad"
max_count [x] = x
max_count (x:xs) = x max_helper (max_count xs)
where
max_helper (a,b) (a',b')
| length b > length b' = (a, length b)
| otherwise = (a', length b')
在此先感谢您提供的任何帮助。
您已定义 list_helper
:
list_helper [] [] = []
list_helper (x:xs) y
| x `elem` y = list_helper xs y
| otherwise = x:(list_helper xs y)
有关 non-exhaustive 模式匹配的警告意味着您的函数正在评估您尚未告诉它如何处理的参数。在这种情况下,第一个参数是一个空列表,但第二个参数 not 是空的。
你需要处理这个模式:
list_helper [] [] = []
list_helper [] y = y
list_helper (x:xs) y
| x `elem` y = list_helper xs y
| otherwise = x:(list_helper xs y)
Prelude> :{
Prelude| list_helper [] [] = []
Prelude| list_helper [] y = y
Prelude| list_helper (x:xs) y
Prelude| | x `elem` y = list_helper xs y
Prelude| | otherwise = x:(list_helper xs y)
Prelude| :}
Prelude> list_helper [1, 2, 3] [4, 5, 6]
[1,2,3,4,5,6]