English 中文(简体)
F# match active pattern as expanded tuple
原标题:

I get the following error in diff with a red squiggle under Subset.
Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice

Is there some sort of type annotation I can add to the SubSet match so I don t have to use fst and snd? If not is there any intention to support this syntax?

type Range = {min : int64; max : int64}

let (|Before|After|BeforeOverlap|AfterOverlap|SuperSet|SubSet|) (x, y) = 
    if x.min > y.max then After
    elif x.min >= y.min then
        if x.max <= y.max then SubSet
        else AfterOverlap
    elif x.max < y.min then Before
    elif x.max <= y.max then BeforeOverlap
    else SuperSet

let useOldx x xe ye = ()

let diff (xe:IEnumerator<Range>) (ye:IEnumerator<Range>) =
    match xe.Current, ye.Current with
    | After as tuple -> ()
    | Before as t -> if xe.MoveNext() then useOldx (fst t) xe ye
    | SuperSet as t -> 
        let x, y = t
        if xe.MoveNext() then useOldx x xe ye
    | SubSet as x, y -> if xe.MoveNext() then useOldx x xe ye
    | _ -> ()
最佳回答

You could do this:

    | (x,y) & SubSet -> if xe.MoveNext() then useOldx x xe ye

or you could do this:

open System.Collections.Generic 

type Range = {min : int64; max : int64}

let (|Before|After|BeforeOverlap|AfterOverlap|SuperSet|SubSet|) (x, y) = 
    if x.min > y.max then After
    elif x.min >= y.min then
        if x.max <= y.max then SubSet(x,y)
        else AfterOverlap
    elif x.max < y.min then Before
    elif x.max <= y.max then BeforeOverlap
    else SuperSet

let useOldx x xe ye = ()

let diff (xe:IEnumerator<Range>) (ye:IEnumerator<Range>) =
    match xe.Current, ye.Current with
    | After as tuple -> ()
    | Before as t -> if xe.MoveNext() then useOldx (fst t) xe ye
    | SuperSet as t -> 
        let x, y = t
        if xe.MoveNext() then useOldx x xe ye
    | SubSet(x, y) -> if xe.MoveNext() then useOldx x xe ye
    | _ -> ()
问题回答

暂无回答




相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签