在Haskell中挣扎着列表 [英] Struggling with lists in Haskell
问题描述
以下是我的函数:
create_matrix ::(Read t,Integral t)=> [字符串] - > t - > [[t]]
create_list_of_lists(x:xs)num = [read x`div`z | z < - [1..num]]:create_list_of_lists xs num
当我像这样运行它时:
create_list_of_lists [1212,3232] 3
我得到所需的输出,即
[ [1212,606,404],[3232,1616,1077]
但错过了最后的']'最后,并向我显示这个错误:
例外:hondt.hs:39:1-81:非穷举在函数中的模式create_list_of_lists
我想它与类型有关,但我不知道我是什么'm做错了。
发生什么事是由于Haskell的懒惰,GHCi将前两个子列表打印为它会收到它们,但会遇到错误。此错误是因为您尚未为 create_list_of_lists
定义所有可能的输入,即当第一个参数为空时。您需要
create_list_of_lists [] num = ???
create_list_of_lists(x:xs)num = [read x`div`z | z< - [1..num]]:create_list_of_lists xs num
c $ c> [] ,你最终会调用 create_list_of_lists [] num
,这当前是未定义的。
I'm having trouble with a list of lists in Haskell.
Here's my function:
create_matrix :: (Read t, Integral t) => [String] -> t -> [[t]]
create_list_of_lists (x:xs) num = [read x `div`z | z <- [1..num]] : create_list_of_lists xs num
When i run it like this:
create_list_of_lists ["1212","3232"] 3
i get sort off the desired output, which is
[[1212,606,404],[3232,1616,1077]
but it misses the last ']' at the end, and presents me with this error:
Exception: hondt.hs:39:1-81: Non-exhaustive patterns in function create_list_of_lists
I guess it has to do with types, but i don't know what i'm doing wrong.
What is happening is that due to Haskell's laziness GHCi is printing out the first two sub-lists as it receives them, but then runs into an error. This error is because you haven't defined all the possible inputs for create_list_of_lists
, namely when the first argument is empty. You'll need
create_list_of_lists [] num = ???
create_list_of_lists (x:xs) num = [read x `div`z | z <- [1..num]] : create_list_of_lists xs num
Without the clause for []
, you'll end up calling create_list_of_lists [] num
, which is currently undefined.
这篇关于在Haskell中挣扎着列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!