1
Seq.pairwise

is the function you are looking for.

1
2
3
4
 

xs |> Seq.pairwise |> Seq.filter (fun ((_, x1, y1, z1), (x2, y2, _, z2)) -> x1=x2 && y1=y2 && z=not z)
By on 11/2/2008 10:15 PM ()

Hello,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#light

type t = int*int*int*bool
type t2t = Map<t, t list>

let f lst =
  let rec aux acc = function
    |[] -> acc
    |((a,x,y,z) as hd)::tl ->
      let k = (x,y,0,not z)
      match Map.tryfind k acc with
        |None ->  aux (Map.add k [hd] acc) tl
        |Some l -> aux (Map.add k (hd::l) acc) tl
  for kv in aux (Map.empty<t,t list>) lst do
    let l = List.rev kv.Value
    printfn "%A: %A" (List.hd l) (List.tl l)
By on 10/31/2008 7:19 AM ()

The following uses no storage but is O(n^2).

1
2
3
4
5
6
7
8
9
10
11
// "Extend" Seq module with a product
module Seq =
    let product xs ys = { for x in xs do for y in ys do yield x,y }

type items = A | B | C | D | E | F | G
let data = [A,B,C,true;  A,B,D,false; A,C,D,true;  A,E,F,true;
            A,C,D,false; F,A,B,false; E,F,G,false; C,D,B,false ]

Seq.product data data |> Seq.filter (fun ((x,y,z,f),(xx,yy,zz,ff)) -> y=xx && z=yy && f=not ff)

By on 10/31/2008 6:34 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper