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.
75 people like thisPosted: 13 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: 13 years ago by Alex Muscar
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.
69 people like thisPosted: 13 years ago by Rick Minerich
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.
54 people like thisPosted: 13 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.
16 people like thisPosted: 13 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: 13 years ago by Carsten König
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: 12 years ago by Nick Palladinos
Convert string to and from character lists.
11 people like thisPosted: 12 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: 12 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: 12 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.
7 people like thisPosted: 11 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.
5 people like thisPosted: 11 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: 11 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).
11 people like thisPosted: 11 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: 10 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.
5 people like thisPosted: 10 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'
1 people like thisPosted: 9 years ago by Tomas Petricek
Lock-free, mutable list that supports multi-threading scenarios.
4 people like thisPosted: 9 years ago by Ruxo Zheng
How to print a formatted calendar to the console using F#
12 people like thisPosted: 9 years ago by Fabio Galuppo
Rotate List
2 people like thisPosted: 9 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.
5 people like thisPosted: 8 years ago by Phillip Trelford
Tail recursive implementation of an intersperse function.
2 people like thisPosted: 7 years ago by Tobias Burger
Shows empty list constructor
2 people like thisPosted: 6 years ago by fsoikin
This snippet declares a function that operates on lists that groups elements of a sequence while the predicate holds. A new group is started for an element when the predicate no longer holds. The predicate is passed the current element and the previous element. This implementation is likely not very efficient.
0 people like thisPosted: 5 years ago by Matthew Rodatus
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.
3 people like thisPosted: 4 years ago by Eugene Gavrin
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: 4 years ago by Rick Minerich
This is a recursive active pattern for extracting the elements of a list quotation as a list of Exprs, There doesn't seem to be a List pattern in the standard library. The compiler will complain about this recursive pattern unless you use #nowarn "40".
4 people like thisPosted: 3 years ago by allisterb
This rotates or shifts a list. Unlike http://www.fssnip.net/qY/title/Rotate-List, which runs exponentially, this runs at O(n). In case of overflow (i.e., shift index is larger than the size of the list) will run at most once more with the modulo. This is done to prevent using `List.length` prior to entering the function, as that would lead to a perf punishment of an extra O(n) on each invocation. For large lists, `shift largeList 1` would then get a big performance hit.
1 people like thisPosted: 1 year ago by Abel Braaksma
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.
70 people like thisPosted: 13 years ago by Tomas Petricek
Computes the Cartesian product of a list of lists. See also corresponding example for a sequence of sequences.
49 people like thisPosted: 13 years ago by Neil Carrier
Function to generate all possible combinations where combination "ab" is different then "ba"
5 people like thisPosted: 13 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.
29 people like thisPosted: 13 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.
80 people like thisPosted: 13 years ago by Dmitri Pavlenkov
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: 13 years ago by Cameron Frederick
A CPS version of FuncList, in order to avoid blowing the stack.
3 people like thisPosted: 12 years ago by Nick Palladinos
An initial attempt at creating a ByteString type based on the Haskell version.
4 people like thisPosted: 12 years ago by Ryan Riley
A function that takes a random subset from a seq<'T>.
1 people like thisPosted: 12 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.
4 people like thisPosted: 12 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: 11 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.
12 people like thisPosted: 11 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: 11 years ago by Joel Huang
A few active pattern combinators useful when pattern matching on lists is necessary.
2 people like thisPosted: 11 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: 10 years ago by devshorts
Prints a list in a spiral
3 people like thisPosted: 10 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: 9 years ago by @BrockSamsonUK
Removes the nth element from a list
2 people like thisPosted: 9 years ago by Antonio Prestes García
Compress List
2 people like thisPosted: 9 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: 9 years ago by Faisal Waris
BELLMAN-FORD algorithm to the shortest path based on pseudo code in Algortihms Unlocked
4 people like thisPosted: 8 years ago by Fabio Galuppo
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.
4 people like thisPosted: 6 years ago by Tuomas Hietanen
Easy way to chunk string into equal segments. Returns a list of string chunks (as opposed to an array or seq of chunks).
7 people like thisPosted: 5 years ago by LSM07
Creates a jagged 2d list of Pascal's Triangle.
2 people like thisPosted: 4 years ago by Kayden Tebau
Playing with coordinates and moves See https://github.com/giuliohome/AdventOfCode2019/blob/master/Day15-Part1-QueueVersion.fs#L54-L66
1 people like thisPosted: 4 years ago by giuliohome
Using phantom types to emulate simple dependent types and sized list. Inspired by: https://www.seas.upenn.edu/~sweirich/cis670/09/
6 people like thisPosted: 3 years ago by Vankata
Another version of QuickSort implementation.
6 people like thisPosted: 2 years ago by Shankar V
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.
0 people like thisPosted: 15 days ago by Tomas Petricek