How to apply Seq display function?

I recently played with F #. I was wondering, instead of using a for loop to generate a sequence for an element that is multiplied by every other element in the list, how can I use the Seq display function or something similar to generate something like below.

So, for example, for. I have a list [1..10] I would like to apply a pleasure that generates a result similar to

[(1*1); (1*2);(1*3); (1*4); (1*5)......(2*1);(2*2);(2*3).....(3*1);(3*2)...]

How can I achieve this?

Thank you very much for everything that will help you.

+3
source share
4 answers
let list = [1..10]

list |> List.map (fun v1 -> List.map (fun v2 -> (v1*v2)) list) |> List.collect id

List.collect at the end aligns the list of lists. It works the same as Seq instead of List if you need a lazy sequence.

, collect , cfern obsessivley, :

let flip f x y = f y x

let list = [1..10]

list |> List.collect ((*) >> ((flip List.map) list))
+6

:

let allpairs L = 
    [for x in L do
        for y in L -> (x*y)]

, :

let pairs2 L = L |> List.collect (fun x -> L |> List.map (fun y -> (x*y)))


: , :

type Microsoft.FSharp.Collections.List<'a> with
    member L.cross f = 
        [for x in L do
            for y in L -> f x y]

:

> [1;2;3].cross (fun x y -> (x,y));;
val it : (int * int) list =
  [(1, 1); (1, 2); (1, 3); (2, 1); (2, 2); (2, 3); (3, 1); (3, 2); (3, 3)]

F # , # 'ish. , , F # , pipe (| > ).

List , :

module List =
    let cross f L1 L2 = 
        [for x in L1 do
            for y in L2 -> f x y]

, cross, List:

> List.cross (fun x y -> (x,y)) [1;2;3] [1;2;3];;
val it : (int * int) list =
  [(1, 1); (1, 2); (1, 3); (2, 1); (2, 2); (2, 3); (3, 1); (3, 2); (3, 3)]
> List.cross (*) [1;2;3] [1;2;3];;
val it : int list = [1; 2; 3; 2; 4; 6; 3; 6; 9]
+5

-:

let cross l1 l2 =
  seq { for el1 in l1 do
          for el2 in l2 do
            yield el1, el2 };;

:

cross [1..10] [1..10] |> Seq.map (fun (a,b) -> a*b) |> Seq.toList
+3

for, , , , :

let cross xs ys =
  let rec crossAux ol2 l1 l2 =
    match l1, l2 with
    // All elements from the second list were processed
    | x::xs, [] -> crossAux ol2 xs ol2             
    // Report first elements and continue looping after
    // removing first element from the second list
    | x::xs, y::ys -> (x, y)::(crossAux ol2 l1 ys) 
    // First list is empty - we're done
    | [], _ -> []
  crossAux ys xs ys

, , , , .

Mau , List.map List.collect id, ( collect). cross (, , ):

let cross xs ys =
  xs |> List.collect (fun v1 -> 
    ys |> List.map (fun v2 -> (v1, v2))) 

By the way, my book avaialable has a free chapter that discusses how sequence expressions and functions work List.collect. It is worth noting that forin the sequence expressions it corresponds List.collect, therefore you can write code only using this function of a higher order:

let cross xs ys =
  xs |> List.collect (fun v1 -> 
    ys |> List.collect (fun v2 -> [(v1, v2)] )) 

However, for more information, see the free chapter :-).

+2
source

Source: https://habr.com/ru/post/1748660/


All Articles