如何创建"配对"功能以匹配字符串列表?

dan*_*die 1 f#

我正在对F#Wiki Book on List进行练习(滚动到底部)来创建一个Pair方法.

我能够没有问题地对整数列表进行配对,但是为字符串列表抛出了F#异常.对于像我这样的F#初学者而言,解读异常意味着什么,这真是太神秘了.

这是我初次尝试实施Pairfsi.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 FSI_0002.clo@2T.Invoke(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
Run Code Online (Sandbox Code Playgroud)

那么Pair在整数版本
和函数签名上工作

val pair : 'a list -> ('a * 'a) list
Run Code Online (Sandbox Code Playgroud)

表示Pair在通用列表上运行.

问题:那为什么Pair不能在字符串列表上工作?

[答案](我的版本)
只需返回else案例(_)的累积列表即可.
并且警告也会得到处理.

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)
Run Code Online (Sandbox Code Playgroud)

[编辑2]嗯,我也会发布我的Unpair完整性版本.

let unpair l = [for (a,b) in l do yield! a :: b :: []]
Run Code Online (Sandbox Code Playgroud)

这里有一些有缺陷的基准测试使用解决方案版本与我的100万项目列表

#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))
Run Code Online (Sandbox Code Playgroud)

基准结果:

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

My version
PAIR -> Duration (ms): 220.000000
UNPAIR -> Duration (ms): 1624.000000
Run Code Online (Sandbox Code Playgroud)

JP *_*oto 6

我认为您的Pair版本不会在奇数个列表上工作.您碰巧测试了偶数个整数和奇数个字符串.我认为匹配的第二个参数意味着至少有两个成员的列表.所以你中断2中断2并获得一个包含1个元素的列表,并且没有任何条件匹配.

[_]是1项目列表,其中包含任何内容.您必须提供与其匹配的谓词.