我正在使用列表并创建许多其他函数来将数字插入此列表中的任何位置。我已经完成了这个代码,但它给了我一些错误:非常感谢任何帮助。
list = [1,2,4,5,6,7]
nspace=3
newvalue_n=3
lenlist=length list
calc1= nspace-1
calc2=lenlist-nspace
new_take=take calc1
new_drop=drop calc2
newinsert=new_take ++ newvalue_n ++ new_drop
答案 0 :(得分:2)
一个不错的开始!当我试用你的文件时,这是我得到的错误。让我们一次拿一个:
test.hs:9:11: error:
• Couldn't match expected type ‘[a]’
with actual type ‘[a0] -> [a0]’
• Probable cause: ‘new_take’ is applied to too few arguments
In the first argument of ‘(++)’, namely ‘new_take’
In the expression: new_take ++ newvalue_n ++ new_drop
In an equation for ‘newinsert’:
newinsert = new_take ++ newvalue_n ++ new_drop
• Relevant bindings include newinsert :: [a] (bound at test.hs:9:1)
嗯,这很清楚。 " new_take
适用于太少的论点",让我们再次看一下new_take
的定义:
new_take=take calc1
啊,对!你可能想要这个:
new_take=take calc1 list
下一个错误:
test.hs:9:23: error:
• Couldn't match expected type ‘[a]’ with actual type ‘Integer’
• In the first argument of ‘(++)’, namely ‘newvalue_n’
In the second argument of ‘(++)’, namely ‘newvalue_n ++ new_drop’
In the expression: new_take ++ newvalue_n ++ new_drop
• Relevant bindings include newinsert :: [a] (bound at test.hs:9:1)
好的,"无法将预期类型[a]
与Integer
"中的newvalue_n
中的实际类型newvalue_n ++ new_drop
匹配。让我们自己提醒一下(++)
的类型以及newvalue_n
的定义:
(++) :: [a] -> [a] -> [a]
newvalue_n=3
我必须同意GHC 3
看起来不像列表。易于修复,让我们把它变成单身人士名单:
newinsert=new_take ++ [newvalue_n] ++ new_drop
最后一个:
test.hs:9:37: error:
• Couldn't match expected type ‘[a]’
with actual type ‘[a1] -> [a1]’
• Probable cause: ‘new_drop’ is applied to too few arguments
In the second argument of ‘(++)’, namely ‘new_drop’
In the second argument of ‘(++)’, namely ‘newvalue_n ++ new_drop’
In the expression: new_take ++ newvalue_n ++ new_drop
• Relevant bindings include newinsert :: [a] (bound at test.hs:9:1)
就像第一个,你可能想要:
new_drop=drop calc2 list
这些是让工作变得有效所需的最小变化。可以做出许多风格上的改进;我建议您将代码发布到Code Review StackExchange,一旦您满意,就可以发布代码。