Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does F# have an equivalent to Haskell's take?

Tags:

list

haskell

f#

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.

like image 608
McMuttons Avatar asked Apr 13 '10 20:04

McMuttons


People also ask

What does F () mean in math?

more ... A special relationship where each input has a single output. It is often written as "f(x)" where x is the input value. Example: f(x) = x/2 ("f of x equals x divided by 2")

Does F mean derivative?

Derivative as a functionLet f be a function that has a derivative at every point in its domain. We can then define a function that maps every point x to the value of the derivative of f at x. This function is written f′ and is called the derivative function or the derivative of f.

What does F say about F and F?

f, f′ and f″ Since (f′)′=f″, when f′ is increasing, f″ is positive. Similarly, when the slopes of tangent lines are decreasing, i.e. when f′ is decreasing, the function is concave down, as you can see in the second two graphs below. Since (f′)′=f″, when f′ is decreasing, f″ is negative.


2 Answers

To clarify a few things, the difference between Seq.take and Seq.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 the Seq.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 the seq<'a> type, which is an interface. The type seq<'a>is actually just a type alias for IEnumerable<'a>, so it is implemented by all other collections (including arrays, mutable lists, etc.). The following code will work fine:

let list = [ 1 .. 10 ]
let res = list |> Seq.take 5

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):

let resList = res |> List.ofSeq

I'm not sure why F# libraries don't provide List.take or List.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 the Seq module (but that's only my guess...)

like image 95
Tomas Petricek Avatar answered Oct 30 '22 14:10

Tomas Petricek


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 List.toSeq first. (Update: Apparently from the comments, this isn't necessary.)

like image 27
Joren Avatar answered Oct 30 '22 13:10

Joren