سؤال

هل هناك رمز أفضل لا يحتاج إلى تحويل التسلسل إلى قائمة؟ giveacodicetagpre.

هل كانت مفيدة؟

المحلول

This is a great place to use Seq.fold. It collapses a collection down into a single value.

Cartesian keys1 keys2
|> Seq.fold (fun map (i, j) ->
  let value = (inputd.[i]).[j]
  Map.add (i, j) value map) Map.empty

نصائح أخرى

Cartesian keys1 keys2
|> Seq.map (fun (i, j) -> ((i, j), (inputd.[i]).[j]))
|> Map.ofSeq

As a complement to the previous answers, if you want to be able to pattern-match sequences, you can define an active pattern:

let (|Cons|Nil|) s =
    if Seq.isEmpty s then
        Nil
    else
        Cons(Seq.head s, Seq.skip 1 s)

let rec addentry map keys  =
    match keys with 
    | Cons((i,j), tail) ->  Map.add (i,j) ((inputd.[i]).[j]) (addentry map tail)
    | Nil -> map
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top