In Haskell, there is a function "take n list" which returns the first n elements from a list. For example "sum (take 3 xs)" sums up the first three elements in the list xs. Does F# have an equivalent? I expected it to be one of the List-functions, but I can't spot anything that seems to match.
相关问题
- how to split a list into a given number of sub-lis
- C#: How do i get 2 lists into one 2-tuple list in
- F#: Storing and mapping a list of functions
- F#: Storing and mapping a list of functions
- Select first row from multiple dataframe and bind
相关文章
- List可以存储接口类型的数据吗?
-
C# List
.FindAll 时 空指针异常 - What is the complexity of bisect algorithm?
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Given a list and a bitmask, how do I return the va
- Top-level expression evaluation at compile time
- FSharp.Data.JsonProvider - Getting json from types
To clarify a few things, the difference between
Seq.take
andSeq.truncate
(as pointed out by @sepp2k) is that the second one will give you a sequence that returns at most the number of elements you specified (but if the length of the sequence is less, it will give you less elements).The sequence generated
Seq.take
function will throw an exception if you try to access an element beyond the length of the original list (Note that theSeq.take
function doesn't throw the exception immediately, because the result is lazily generated sequence).Also, you don't need to convert the list to a sequence explicitly. Under the cover,
list<'a>
is a .NET class that inherits from theseq<'a>
type, which is an interface. The typeseq<'a>
is actually just a type alias forIEnumerable<'a>
, so it is implemented by all other collections (including arrays, mutable lists, etc.). The following code will work fine:However, if you want to get a result of type
list<int>
you'll need to convert sequence back to a list (because a list is more specific type than a sequence):I'm not sure why F# libraries don't provide
List.take
orList.truncate
. I guess the goal was to avoid reimplementing the whole set of functions for all types of collections, so those where the implementation for sequences is good enough when working with a more specific collection type are available only in theSeq
module (but that's only my guess...)Seq.take works, as others already have stated, but all Seq operations on a list come at a cost. In the case of Seq.take, it's not surprising, as the list has to be copied.It's more noteworthy that, for example, Seq.concat on a list takes a lot more time than List.concat.
I suppose that implies that you don't just access the list as a seq when you call a Seq.xxx function, but that the list in copied/converted to a Seq behind the scenes as well.edit: The reason I drew the conclusion above, was this bench using F# interactive:
On my machine, the
List.length
version takes about 1.9 secs, whereas theSeq.length
version takes about 3.8 secs (shortest time of a few repeated tests of the length lines only, excluding the list generation line).Yeah, it's called
Seq.take
. Usage seems to be identical to Haskell's:Seq.take
count source.To use it on a list, use(Update: Apparently from the comments, this isn't necessary.)List.toSeq
first.