Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match the first two elements of a list

Tags:

f#

I have a list :

[objA;objB;objC;objD]

I need to do the following reduction :

obj -> obj -> obj

ie :

objA objB -> objB'

and then take back the original list so that I get :

[objB';objC;objD]

I am trying to do the following :

let rec resolveConflicts = function
    | [] -> []
    | [c] -> resolveConflict c
    | [x;y] -> resolveConflict x
               |> List.filter getMovesRelatedtoY
               |> List.append y
               |> resolveConflict 
    | [x;y::tail] -> 
               let y' = resolveConflict x
                        |> List.filter getMovesRelatedtoY
                        |> List.append y
               resolveConflicts y'::tail

This syntax is not correct, may be I am not even using the correct tool... I am open to any well suited solution so that I can learn.

As to why, I filter the list and append one to another, it is just that every conflict is a list of moves.

like image 883
Arthis Avatar asked Jun 18 '14 09:06

Arthis


Video Answer


1 Answers

To match first element, second element and the rest of the list, use this pattern:

| fst::snd::rest ->

You can match any constant number of first elements using this styling:

| fst::snd::thrd:: ... ::rest ->
like image 126
Savenkov Alexey Avatar answered Oct 10 '22 16:10

Savenkov Alexey