我是haskell的新手,负责创建一个函数,它接受一个int和一个int列表,函数会查找输入的ints位置并返回它之前的值,例如fn 5 [1,2,3,4,5,6]会返回4.我有很多问题入门。首先我不断收到变量不在范围错误。Haskell递归函数和语法
fn' ::Int->[Int]->Int
fn' y [] = -1
fn' y (x:xs)
|y = (head listail) = x
|otherwise = listail
where listail = fn' y (tail)xs
我应该从哪里开始看,而且一般情况下还有其他事情我应该或不应该做?
亚当斯代码错误
main.hs:3:31: error:
• Couldn't match expected type ‘Int’ with actual type ‘[Int]’
• In the expression: fn y x2 : xs
In an equation for ‘fn’:
fn y (x1 : x2 : xs)
| y == x2 = x1
| otherwise = fn y x2 : xs
main.hs:3:36: error:
• Couldn't match expected type ‘[Int]’ with actual type ‘Int’
• In the second argument of ‘fn’, namely ‘x2’
In the first argument of ‘(:)’, namely ‘fn y x2’
In the expression: fn y x2 : xs
<interactive>:3:1: error:
• Variable not in scope: main
• Perhaps you meant ‘min’ (imported from Prelude)
什么是'findNext''? –
由于您没有列出错误信息,所以难以说出您的问题所在,但我保证您的“变量不在范围内”是来自“findNext”错字。注:也是'y =(head listail)'是赋值。你想'y ==(head listail)'(尽管'listail'似乎被定义为错误。) –