Snippets in category Lists
Filtering lists
Two functions showing how to filter functional lists using the specified predicate. First version uses naive recursion and the second one is tail-recursive using the accumulator parameter.
39 people like this
Posted: 1 years ago by Tomas PetricekProjecting lists
Three functions showing how to implement projection for functional lists. First version uses naive recursion and the second one is tail-recursive using the accumulator parameter. The third version extends this with continuation passing.
43 people like this
Posted: 1 years ago by Tomas PetricekPipeline list processing
An example showing how to process list in a pipeline. We first use List.filter to return only even numbers and then use List.map to format them as strings.
76 people like this
Posted: 1 years ago by Tomas PetricekComposing a list of functions
Composition of functions in F# is easily achieved by using the >> operator. You can also chain an arbitary amount of functions (represented as a list or sequence) together by folding the list/seq with >>. [More formally: the set of endomorphisms 'a -> 'a forms a monoid with the binary, associative operator ">>" (or "<<") and the neutral element "id".]
59 people like this
Posted: 1 years ago by NovoxAssociation list lookup
While prototyping programs I find myself using association lists. This little snippet defines a lookup functions for association lists defined as lists of tuples.
47 people like this
Posted: 1 years ago by Alex MuscarProject Euler #1
This snippet is code that solves first Project Euler problem. It finds the sum of all the multiples of 3 or 5 below 1000. Please add other (more efficient, succinct or interesting) solutions to this snippet.
37 people like this
Posted: 1 years ago by Eugene GavrinCartesian Product of Lists
Computes the Cartesian product of a list of lists. See also corresponding example for a sequence of sequences.
34 people like this
Posted: 1 years ago by Neil CarrierTraverse quotation
Shows how to use the 'ExprShape' module to recursively traverse an entire quotation and how to write quotation transformations. As an example, the snippet replaces all numeric constants in the quotation and then runs the transformed code.
13 people like this
Posted: 1 years ago by Tomas PetricekPartition a sequence until a predicate is satiated
This function is given a partition predicate and a sequence. Until the predicate returns false, a list will be filled with elements. When it is, both the list and the remainder of the sequence will be returned. Note that this example preserves the laziness of the unchecked sequence elements.
38 people like this
Posted: 1 years ago by Rick MinerichFunction to get all possible combinations
Function to get all possible combinations of list items. There are some Euler problems (like 77 & 78) to get total amounts. But e.g. for some card game implementations you will need the real items.
27 people like this
Posted: 1 years ago by Tuomas HietanenFunction to generate all possible combinations where combination "ab" != "ba"
Function to generate all possible combinations where combination "ab" is different then "ba"
5 people like this
Posted: 1 years ago by Ankur DhamaRemove first ocurrence from list
Removes from list the first ocurrence only of an element that satisfies the predicate. Additional elements that also satisfy the predicate remain in the list.
32 people like this
Posted: 1 years ago by Alexander RautenbergPascal's Triangle
This code sample generates Pascal's triangle as jagged 2D list (list of lists).
13 people like this
Posted: 1 years ago by Dmitry SoshnikovPascal's Triangle (2)
This code sample generates Pascal's triangle as jagged 2D list (list of lists). It takes 0.5 sec to generate 3000 rows (vs 16 sec for http://fssnip.net/23). Tip: try to avoid of using "list1 @ list2" construction.
8 people like this
Posted: 1 years ago by Shamil SayfutdinovSorted Map
Sorted Map
13 people like this
Posted: 1 years ago by fholmAll combinations of list elements
For a given list, find all possible combinations of elements of the list (not just k-combinations). The result is a list of lists with each element representing one combination. Order of elements is not taken into account.
14 people like this
Posted: 1 years ago by Alexander RautenbergCartesian product of n lists
Cartesian product of a variable number of lists. Input is a list of lists of which the cartesian product is to be constructed; output is a list that contains the elements of the product set, as lists.
11 people like this
Posted: 1 years ago by Alexander RautenbergPalindromic dates
Today, 11. february 2011, is a palindromic day according to the European date format (day/month/year). This snippet collects all the palindromic dates until 31 dec. 9999. They are 366, a surprisingly low number.
12 people like this
Posted: 1 years ago by Francesco De VittoriPascal's Triangle
Returns the pascal triangle in a 2D list . This snippet computes rows translating the 'visual' method taught at school into Lists and the usage of map2 function. It takes almost 5 seconds to compute 5000 rows.
12 people like this
Posted: 1 years ago by Horacio NuñezExtensions to the Fold function
This snippet is helpfull in the following cases: 1) After a consolidation operation using the fold function we need to know how many elements have been processed. 2) A consolidation operation needs to use the index of each of the elements processed and we don't want to use the mapi function first. 3) A combination of the above. Since the following snippet just adds a wrapper to the existing Fold function we can repeat the approach for arrays and sequences (including the plinq ones)
22 people like this
Posted: 1 years ago by Horacio NuñezWicked way to solve quadratic equation using list of operators
This is to demonstrate that: (1) there are many ways to solve the same problems; (2) operators can be grouped together into data structures and act as data; (3) you can have fun in F# in many ways.
49 people like this
Posted: 1 years ago by Dmitry SoshnikovSplit a list
Three ways to split a list in half (but not necessarily in the middle). A forth version added that's very short and should be fast, as we only use List.fold. New champ found.
48 people like this
Posted: 1 years ago by Dmitri Pavlenkov
Chronological sequence window beginnings
An abstraction of the following use case: Given a sequence of dates and max temperatures for each date, extract out the initial dates on which the temp is greater than a given threshold for n consecutive days. (Originally posted as an answer to this StackOverflow question: http://stackoverflow.com/questions/5267055 )
10 people like this
Posted: 1 years ago by ildjarnPermutations
computes the list of all permutations of a list for example the permutations of [1;2;3] will be [1;2;3]; [1;3;2]; [2;1;3]; [2;3;1]; [3;1;2]; [3;2;1]
2 people like this
Posted: 1 years ago by Carsten KönigVery Fast Permutations
I spent a lot of time this week profiling different permutation functions from various places on the internet. The following was by far the fastest:
4 people like this
Posted: 1 years ago by Rick MinerichPython's sorted function
This is an attempt to produce similar behavior as seen in the sorted( ) function in Python. Supporting only one of the three optional arguments. The key - Specifies a function of one argument that is used to extract a comparison key from each list element
2 people like this
Posted: 1 years ago by Cameron FrederickHughes's FuncList
A FuncList is a "list-like" datatype with constant time append (represented as a function of cons-lists). The implementation is based on a convenient computation builder.
1 people like this
Posted: 11 months ago by Nick PalladinosHughes's CPSFuncList
A CPS version of FuncList, in order to avoid blowing the stack.
3 people like this
Posted: 11 months ago by Nick PalladinosString explode and implode
Convert string to and from character lists.
3 people like this
Posted: 11 months ago by petebuSeq.sumBy implemented in terms of Seq.fold
Seq.sumBy implemented in terms of Seq.fold
2 people like this
Posted: 11 months ago by ildjarnByteString
An initial attempt at creating a ByteString type based on the Haskell version.
1 people like this
Posted: 10 months ago by Ryan RileyJoinList
A JoinList is a variation on the list type that offers a constant time append operation.
5 people like this
Posted: 9 months ago by Ryan RileyEuler #5
Euler #5 solution
3 people like this
Posted: 8 months ago by Michael FalangaPack consecutive duplicates of list elements into sublists
Pack consecutive duplicates of list elements into sublists.If a list contains repeated elements they should be placed in separate sublists.
0 people like this
Posted: 8 months ago by Naveenipv4 conversion snippet
ipv4 conversion snippet, updated suggestions based on Paks's comments on fsharp irc.
4 people like this
Posted: 6 months ago by david kleinFolding over prime factors
Let's have some fun with higher order functions and instead of folding over a list, fold over the prime factors of a number. It can be optimized further by dividing out real primes instead of numbers of the form 6k+/-1, but it's not embarrassingly slow.
0 people like this
Posted: 6 months ago by Arjen KopingaRandom Subset
A function that takes a random subset from a seq<'T>.
0 people like this
Posted: 5 months ago by Taha Hachanan-ary Seq.map
A pattern for creating n-ary Seq.map functions.
2 people like this
Posted: 4 months ago by Nick Palladinosn-ary Seq.map (Numerals)
A pattern for creating n-ary Seq.map functions, based on numerals.
2 people like this
Posted: 4 months ago by Nick PalladinosNest items of a sequence
A function that nests items of a sequence that do not match a specified predicate under the last item that matches the predicate. The input is a sequence of values and the result is a sequence of pairs where the second element is a list of nested items.
3 people like this
Posted: 4 months ago by Tomas PetricekNinety-Nine F# Problems - Problems 1 - 10 - Lists
These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.
4 people like this
Posted: 3 months ago by Cesar MendozaNinety-Nine F# Problems - Problems 11 - 20 - List, continued]
These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.
3 people like this
Posted: 3 months ago by Cesar MendozaNinety-Nine F# Problems - Problems 21 - 28 - Lists again
These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.
3 people like this
Posted: 3 months ago by Cesar MendozaRemove duplicate list elements
Remove duplicate elements of a list. Returns list with first instance of duplicate elements, without modification of order except subsequent duplicate elements are omitted.
1 people like this
Posted: 2 months ago by visProcessEngg