我是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'似乎被定義爲錯誤。) –