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.
64 people like thisPosted: 6 years ago by Tomas Petricek
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.
86 people like thisPosted: 6 years ago by Tomas Petricek
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.
71 people like thisPosted: 6 years ago by Eugene Gavrin
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.
67 people like thisPosted: 6 years ago by Rick Minerich
Function to generate all possible combinations where combination "ab" is different then "ba"
5 people like thisPosted: 6 years ago by Ankur Dhama
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.
26 people like thisPosted: 5 years ago by Alexander Rautenberg
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.
75 people like thisPosted: 5 years ago by Dmitri Pavlenkov
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:
8 people like thisPosted: 5 years ago by Rick Minerich
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 thisPosted: 5 years ago by Nick Palladinos
Convert string to and from character lists.
7 people like thisPosted: 5 years ago by petebu
Pack consecutive duplicates of list elements into sublists.If a list contains repeated elements they should be placed in separate sublists.
0 people like thisPosted: 5 years ago by Naveen
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 thisPosted: 5 years ago by Tomas Petricek
The snippet implements 'List.partitionWhile' which behaves as a combination of 'Seq.takeWhile' and 'Seq.skipWhile': It splits the list into a part containing elements from the beginning of a list that match a given predicate and remaining elements.
5 people like thisPosted: 4 years ago by Tomas Petricek
Merge Sort falls into 'Divide and Conquer' problem solving technique and it is a stable sorting. The worst case of running time is (nlogn). This implementation below follows the two abstract steps to achieve Merge Sort, i.e., * Recursively divide input list into two sub-lists. * Repeatedly merge the sub-lists.
3 people like thisPosted: 4 years ago by Joel Huang
This function splits a sequence into lists of length n until there is less than n elements then those are returned.
2 people like thisPosted: 4 years ago by Taha Hachana
According to Wikipedia, the maximum sub-array problem is a programming task of finding the contiguous sub-array within a one-dimensional array of numbers (containing at least one positive number) which has the largest sum. The following is an attempt to solve this problem by using F# list rather than array.
3 people like thisPosted: 4 years ago by Joel Huang
A few active pattern combinators useful when pattern matching on lists is necessary.
0 people like thisPosted: 3 years ago by Eirik Tsarpalis
Fold a string with one delimiter except for the last with a different delim. So basically if you have a list ["1";"2";"3"] and you want it to be ["1";",";"2";"and";"3]
1 people like thisPosted: 3 years ago by devshorts
Prints a list in a spiral
3 people like thisPosted: 2 years ago by devshorts
Needed to partition a list into multiple groups, but couldn't find an existing way to do it. Have not put this as an extension method as needed it in an .fsx file which is loaded, but couldn't get extension method to work from that.
1 people like thisPosted: 2 years ago by @BrockSamsonUK
Removes the nth element from a list
2 people like thisPosted: 2 years ago by Antonio Prestes García
Shows empty list constructor
2 people like thisPosted: 1 years ago by fsoikin
Rotate List
2 people like thisPosted: 1 years ago by Pitch Rock
Extension methods to List<'T> (aliased ResizeArray<'T> in F#) that provide optimised item removal for unordered lists where item order is not significant.
4 people like thisPosted: 11 months ago by Phillip Trelford
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.
63 people like thisPosted: 6 years ago by Tomas Petricek
While prototyping programs I find myself using association lists. This little snippet defines a lookup functions for association lists defined as lists of tuples.
68 people like thisPosted: 6 years ago by Alex Muscar
Computes the Cartesian product of a list of lists. See also corresponding example for a sequence of sequences.
47 people like thisPosted: 6 years ago by Neil Carrier
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.
46 people like thisPosted: 6 years ago by Tuomas Hietanen
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.
52 people like thisPosted: 6 years ago by Alexander Rautenberg
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.
12 people like thisPosted: 5 years ago by Alexander Rautenberg
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 thisPosted: 5 years ago by Carsten König
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 thisPosted: 5 years ago by Cameron Frederick
A CPS version of FuncList, in order to avoid blowing the stack.
3 people like thisPosted: 5 years ago by Nick Palladinos
An initial attempt at creating a ByteString type based on the Haskell version.
3 people like thisPosted: 5 years ago by Ryan Riley
A function that takes a random subset from a seq<'T>.
0 people like thisPosted: 5 years ago by Taha Hachana
I have been working on my "Functional Style" and like to think that I have improved a bit. The list order is no longer maintained in this snippet; however as the order change is simply a feature of the foldBack process the reversal of input and reversal of output is a reliable (and efficient?) fix.
2 people like thisPosted: 4 years ago by visProcessEngg
Lists are pointers to the head of list. It can be defined by a discriminated union type. Using continuation can do a tail-recursion version of appending two lists.
3 people like thisPosted: 4 years ago by Joel Huang
Another version of QuickSort implementation.
2 people like thisPosted: 4 years ago by Joel Huang
An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices. The following is an implementation of such equilibrium list (given the input is a list).
1 people like thisPosted: 4 years ago by Joel Huang
The example shows how to extend F# 3.0 'query' builder with a new custom operation that will work with standard lists, but also with queries that are translated using LINQ expressiont trees (such as databases).
10 people like thisPosted: 4 years ago by Tomas Petricek
See http://stackoverflow.com/questions/18595597/is-using-a-stringbuilder-a-right-thing-to-do-in-f
1 people like thisPosted: 3 years ago by Ramon Snir
Function to combine the values of a list of sequences into a sequence of lists where each list contains the nth element of the different sequences. It works like zip but for an arbitrary number of sequences and it returns lists instead of tuples. As with zip when one sequence is exhausted any remaining elements in the other sequences are ignored.
4 people like thisPosted: 3 years ago by Samuel Bosch
Split a list into chunks using the specified separator. This takes a list and returns a list of lists (chunks) that represent individual groups, separated by the given separator 'v'
0 people like thisPosted: 2 years ago by Tomas Petricek
Lock-free, mutable list that supports multi-threading scenarios.
3 people like thisPosted: 2 years ago by Ruxo Zheng
How to print a formatted calendar to the console using F#
12 people like thisPosted: 1 years ago by Fabio Galuppo
Compress List
2 people like thisPosted: 1 years ago by Pitch Rock
Given a list of lists, the function crossJoin outputs all combination of elements from the inner lists, i.e. each combination is a list which contains elements from each of the inner lists in the input. Note: Order is not preserved
3 people like thisPosted: 1 years ago by Faisal Waris
BELLMAN-FORD algorithm to the shortest path based on pseudo code in Algortihms Unlocked
4 people like thisPosted: 8 months ago by Fabio Galuppo