F # why can't I access the item item

In F #, why can't I access the "Item" element in the array here:

let last (arr:System.Array) = let leng = arr.Length arr.[leng-1] // Error: Field, constructor or member "Item" is not defined. 
+3
source share
3 answers

Can you try this?

 let last (arr:_[]) = let leng = arr.Length arr.[leng-1] 
+5
source

This seems to be a common thing. Raising the documentation , I see

The Array class is a base class for language implementations that supports arrays. However, you can only get the system and compilers explicitly from the Array class. Users should use the array of constructs provided by the language.

+3
source

Also note that in F # you usually use an immutable List:

 let last (stuff: _ list) = let l = stuff.Length stuff.[l] 

But if you use a list, you would use a more efficient algorithm ; F # lists are linked lists:

 let rec last = function | hd :: [] -> hd | hd :: tl -> last tl | _ -> failwith "Empty list." 
0
source

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


All Articles