2017-10-12 80 views
1

我是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) 
+0

什麼是'findNext''? –

+0

由於您沒有列出錯誤信息,所以難以說出您的問題所在,但我保證您的「變量不在範圍內」是來自「findNext」錯字。注:也是'y =(head listail)'是賦值。你想'y ==(head listail)'(儘管'listail'似乎被定義爲錯誤。) –

回答

3

您可以使用模式匹配從列表中搶出兩個值並加以比較。

fn :: Int -> [Int] -> Int 
fn y (x1:x2:xs) | y == x2 = x1 
       | otherwise = fn y (x2:xs) 
fn _ _ = -1 

注意我的最後一種情況 - 這是失敗的情況下,當你無法匹配的模式(x1:x2:xs)

或者:(x1:x2:xs)也可拼寫成(x1:[email protected](x2:_))。後者模式是更復雜的閱讀,但讓你做:

fn :: Int -> [Int] -> Int 
fn y (x1:[email protected](x2:_)) | y == x2 = x1 
        | otherwise = fn y xs 
fn _ _ = -1 

,而不是重新加入x2xs遞歸。

Try it online!


由於Gallais的在評論中指出:

注意,此功能可以採取更多的多晶型Eq a => a -> [a] -> a。這僅僅是一個變化的類型簽名

fn :: Eq a => a -> [a] -> a 

這允許您使用fn與其他有用的類型,即fn '#' "I'm #1!"'1'

而且,這裏一個更好的返回值可能是一個Maybe Int(或一個Maybe a多態形式),因爲你會有一些不包含搜索詞的列表。

fn :: Eq a => a -> [a] -> Maybe a 
fn y (x1:[email protected](x2:_)) | y == x2 = Just x1 
        | otherwise = fn y xs 
fn _ _ = Nothing 
+0

由於學校的限制,我目前使用的是haskell的在線ide,我仍然給了我一個「Variable not in scope error 「以及」無法匹配類型「Int」與「[Int] - > Int」消息。這只是我的IDE的錯誤b/c,如果這是我的原始代碼做什麼? – STRAN

+0

@STRAN不知道你在說什麼變量,你正在使用什麼IDE,以及運行什麼代碼(以及如何)都無法分辨出來。 –

+0

即時通訊使用repl.it,我試圖運行你的代碼,我編輯錯誤消息到後 – STRAN