tags:

views:

201

answers:

1

I was doing an exercise on F# Wiki Book on List (scroll to the bottom) to create a Pair method.

I was able to pair a integer list without problem but an F# exception was thrown for a string list. It is just too cryptic for me to decipher what the exception means for an F# beginner like me.

Here is my initial attempt to implementing Pair on fsi.exe

> let pair l =
-     let rec loop acc = function
-         | [] -> acc
-         | (hd1 :: hd2 :: tl) -> loop ((hd1, hd2) :: acc) tl
-     List.rev(loop [] l)
-
- printfn "%A" ([1..10] |> pair)
- printfn "%A" ([ "one"; "two"; "three"; "four"; "five" ] |> pair);;

      let rec loop acc = function
  -----------------------^

stdin(2,24): warning FS0025: Incomplete pattern matches on this expression. 
 For example, the value '[_]' will not be matched

val pair : 'a list -> ('a * 'a) list

[(1, 2); (3, 4); (5, 6); (7, 8); (9, 10)]
Microsoft.FSharp.Core.MatchFailureException: 
Exception of type 'Microsoft.FSharp.Core.MatchFailureException' was thrown.
   at [email protected](List`1 acc, List`1 _arg1)
   at FSI_0002.pair[T](List`1 l)
   at <StartupCode$FSI_0002>.$FSI_0002._main()
stopped due to error

So Pair does work on integer version
and the function signature

val pair : 'a list -> ('a * 'a) list

indicates that Pair operates on a generic list.

Question: Then why would Pair not work on a string list?

[ANSWER] (my version)
Simply returning accumulated list for else case (_) did the trick.
And the warning is taken care of, as well.

let pair l =
    let rec loop acc = function
//        | [] -> acc
        | (hd1 :: hd2 :: tl) -> loop ((hd1, hd2) :: acc) tl
        | _ -> acc
    List.rev(loop [] l)

printfn "%A" ([1..10] |> pair)
printfn "%A" ([ "one"; "two"; "three"; "four"; "five" ] |> pair)

[EDIT2] Well, I will also post my version of Unpair for completeness.

let unpair l = [for (a,b) in l do yield! a :: b :: []]

Here is somewhat flawed benchmarking using solution version against that of mine for 1 million item lists

#light

open System;

let pn l = printfn "%A" l

let duration f = 
    let startTime = DateTime.Now;
    let returnValue = f()
    let endTime = DateTime.Now;
    printfn "Duration (ms): %f" (endTime - startTime).TotalMilliseconds
    returnValue

let ll =  [for a in 1..1000000 do yield (a)]
let tl = [for a in 1..1000000 do yield (a,a)]


let pair1 l =
    let rec loop acc = function
        | [] | [_] -> List.rev acc
        | h1 :: h2 :: tl -> loop ((h1, h2) :: acc) tl
    loop [] l

let unpair1 l =
    let rec loop acc = function
        | [] -> List.rev acc
        | (h1, h2) :: tl -> loop (h2 :: h1 :: acc) tl
    loop [] l

let pair2 l =
    let rec loop acc = function
        | (hd1 :: hd2 :: tl) -> loop ((hd1, hd2) :: acc) tl
        | _ | [_] -> acc
    List.rev(loop [] l)

    let unpair2 l = [for (a,b) in l do yield! a :: b :: []]

pn(duration (fun() -> ll |> pair1))
pn(duration (fun() -> tl |> unpair1))

pn(duration (fun() -> ll |> pair2))
pn(duration (fun() -> tl |> unpair2))

Benchmark Result:

Solution version
PAIR -> Duration (ms): 255.000000
UNPAIR -> Duration (ms): 840.000000

My version
PAIR -> Duration (ms): 220.000000
UNPAIR -> Duration (ms): 1624.000000
+6  A: 

I don't think your version of Pair would work on a list of an odd number of anything. You happen to test an even number of ints and a odd number of strings. I think your second argument to match implies a list of at least two members. So you break off 2 break off 2 and get to a list with 1 element and none of your conditions match.

[_] is a 1 item list with anything in it. You must provide a predicate that matches it.

JP Alioto
I just returned whatever the accumulated value is returned for "else" case. It works! Thanks - Answer is posted in the question.
Sung Meister