2016-08-24 91 views
1

我在psci repl中執行此操作。這是我的函數:如何在purescript中匹配列表頭部和尾部

$> import Data.List 
$> let f (x:xs) = x 
$> let f Nil = Nil 

$> let a = 1:2:3:Nil 

$> f a 

我得到這個錯誤:

Could not match type 

    Int 

    with type 

    List t0 


while trying to match type List Int 
    with type List (List t0) 
while checking that expression a 
    has type List (List t0) 
in value declaration it 

where t0 is an unknown type 

我理解這種情況正在發生,因爲我的功能f沒有返回List Int簽名。但是,我如何在repl中聲明一個?

回答

1

這裏的問題是f正在返回兩種不同類型:

let f (x:xs) = x 

被返回列表(所以a,或在f a情況Int)的一個元素,而

let f Nil = Nil 

正在返回List

+0

我該如何解決這個問題,以便'f'返回'Maybe'類型? – dopatraman

+0

'f(x:xs)= Just x' 'f Nil = Nothing' –