2012-06-26 33 views
1

試圖在F#中合併兩個LazyLists之後this問題。最初寫作匹配兩個列表。修改,以得到它:與F中的LazyList匹配的模式中的錯誤#

let rec printLazyList (l1:LazyList<int>) (l2:LazyList<int>) = 
    match (l1, l2) with 
     | t, s when t |> LazyList.isEmpty && s |> LazyList.isEmpty -> printfn ""; 
     | t , LazyList.Cons(h2,t2) when t |> LazyList.isEmpty -> printf "%d " h2 
                   let a = LazyList.empty 
                   printLazyList a t2 
     | LazyList.Cons(h1,t1), s when s |> LazyList.isEmpty -> printf "%d " h1 
                   let b = LazyList.empty 
                   printLazyList t1 b 
     | LazyList.Cons(h1,t1), LazyList.Cons(h2,t2) -> if h1 = h2 then 
                  printf "%d " h1 
                  printLazyList t1 t2 
                 elif h1 < h2 then 
                  printf "%d " h1 
                  printLazyList t1 l2 
                 else 
                  printf "%d " h2 
                  printLazyList l1 t2 

問題是它沒有輸出。沒有條件得到滿足(通過在模式匹配的末尾加上|_,_ printfn "nothing matches"進行檢查)在LazyList中使用的cons與通常的F#列表中的::有什麼根本區別嗎?因爲這適用於普通列表(參見上面的鏈接)。

很抱歉,如果這又是一個真正的n00b問題。FP正在尋找比較困難,在這一點上。

回答

5

如果測試使用fsi的功能,要小心,因爲有很多的噪音在fsi控制檯和你的眼睛可能會跳過輸出線

這裏

#r "FSharp.PowerPack.dll" 

open LazyList 

let rec printLazyList l1 l2 = 
    match l1, l2 with 
    | Nil, Nil -> printfn "" 
    | Nil, Cons(h2, t2) -> 
     printf "%d " h2 
     printLazyList LazyList.empty t2 
    | Cons(h1, t1), Nil -> 
     printf "%d " h1 
     printLazyList t1 LazyList.empty 
    | Cons(h1, t1), Cons(h2, t2) when h1 = h2 -> 
     printf "%d " h1 
     printLazyList t1 t2 
    | Cons(h1, t1), Cons(h2, t2) when h1 < h2 -> 
     printf "%d " h1 
     printLazyList t1 l2 
    | Cons(h1, t1), Cons(h2, t2) -> 
     printf "%d " h2 
     printLazyList l1 t2 

let x = LazyList.ofList [1; 2];; 
let y = LazyList.ofList [3; 4];; 
printLazyList x y;; 
+0

混賬:是一個完整的測試(我用Nil代替isEmpty和重組模式的可讀性匹配做了一些清理)。感謝主要的清理,善良知道我是如何錯過它在實驗室! – AruniRC