2012-06-26 2 views
1

this 질문 이후에 F #에서 두 개의 지연 목록을 병합하려고합니다. 원래 두 목록 일치에 대해 썼습니다. 수정 한 내용은 다음과 같습니다.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"을 넣어 확인하십시오.) cons과 LazyList의 차이점은 보통 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

이놈 : 완전한 시험이다 (I 패턴 가독성을 위해 일치를 Nil 대신 isEmpty을 사용하고 재구성하여 몇 가지 정리를했다). 주로 청소를 해줘서 고마워, 선생님이 내가 실험실에서 그것을 어떻게 놓쳤는 지 안다! – AruniRC