How is list implementation implemented in F #?

I'm curious how the module / list type in F # works, in particular, does it optimize this?

let xs = ["1"; "2"; "3"]

let ys = "0"::xs

let zs = ["hello"; "world"]@xs

I looked at the source part https://github.com/fsharp/fsharp/blob/68e37d03dfc15f8105aeb0ac70b846f82b364901/src/fsharp/FSharp.Core/prim-types.fs#L3493 , it seems to be in the corresponding area.

I would like to know if it was copied xsat creation ys.

I would have thought it was just easy to point to an existing list if you just carry an element.

If you are concatenating, I suppose this may not be possible, since this would require changing the last element of the list to point to the next?

If someone can annotate / paste code snippets from FSharp.Core, that would be ideal.

+4
1

, List . . :

type 'T list =
| ([])  
| (::)  of 'T * 'T list 

, :: , ( , ).

@ . :

    let (@) l1 l2 = 
        match l1 with
        | [] -> l2
        | (h::t) -> 
        match l2 with
        | [] -> l1
        | _ -> 
          let res = [h] 
          let lastCons = PrivateListHelpers.appendToFreshConsTail res t 
          PrivateListHelpers.setFreshConsTail lastCons l2;
          res

. appendToFreshConsTail . setFreshConsTail , l2, [], .

+8

All Articles