## Snippets tagged list

• ### 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.

75 people like this

Posted: 13 years ago by Tomas Petricek

• ### Association 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.

68 people like this

Posted: 13 years ago by Alex Muscar

• ### Partition 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.

69 people like this

Posted: 13 years ago by Rick Minerich

• ### Remove 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.

54 people like this

Posted: 13 years ago by Alexander Rautenberg

• ### Cartesian 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.

16 people like this

Posted: 13 years ago by Alexander Rautenberg

• ### Permutations

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: 13 years ago by Carsten König

• ### Hughes'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: 13 years ago by Nick Palladinos

• ### String explode and implode

Convert string to and from character lists.

11 people like this

Posted: 13 years ago by petebu

• ### Pack 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: 13 years ago by Naveen

• ### Nest 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: 12 years ago by Tomas Petricek

• ### Partition a list

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 this

Posted: 12 years ago by Tomas Petricek

• ### Merge Sort on List

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 this

Posted: 11 years ago by Joel Huang

• ### An equilibrium list

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 this

Posted: 11 years ago by Joel Huang

• ### Extending the standard query builder

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 this

Posted: 11 years ago by Tomas Petricek

• ### List-consing String Builder

See http://stackoverflow.com/questions/18595597/is-using-a-stringbuilder-a-right-thing-to-do-in-f

1 people like this

Posted: 11 years ago by Ramon Snir

• ### Convert list of sequences to sequence of lists

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 this

Posted: 10 years ago by Samuel Bosch

• ### Split a list using a separator

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 this

Posted: 10 years ago by Tomas Petricek

• ### Lock-free, mutable list

Lock-free, mutable list that supports multi-threading scenarios.

4 people like this

Posted: 9 years ago by Ruxo Zheng

• ### Print a calendar

How to print a formatted calendar to the console using F#

12 people like this

Posted: 9 years ago by Fabio Galuppo

• ### Rotate List

Rotate List

2 people like this

Posted: 9 years ago by Pitch Rock

• ### List Remove trick

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 this

Posted: 8 years ago by Phillip Trelford

• ### Intersperse a list

Tail recursive implementation of an intersperse function.

2 people like this

Posted: 7 years ago by Tobias Burger

• ### empty list constructor

Shows empty list constructor

2 people like this

Posted: 6 years ago by fsoikin

• ### List.groupWhile function

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 this

Posted: 5 years ago by Matthew Rodatus

• ### Project 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.

3 people like this

Posted: 4 years ago by Eugene Gavrin

• ### Very 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:

8 people like this

Posted: 4 years ago by Rick Minerich

• ### Recursive active pattern for expanding list quotations

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 this

Posted: 3 years ago by allisterb

• ### Rotate or shift lists

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 this

Posted: 2 years ago by Abel Braaksma

• ### Projecting 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.

73 people like this

Posted: 13 years ago by Tomas Petricek

• ### Cartesian Product of Lists

Computes the Cartesian product of a list of lists. See also corresponding example for a sequence of sequences.

49 people like this

Posted: 13 years ago by Neil Carrier

• ### Function to generate all possible combinations where combination "ab" != "ba"

Function to generate all possible combinations where combination "ab" is different then "ba"

6 people like this

Posted: 13 years ago by Ankur Dhama

• ### All 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.

29 people like this

Posted: 13 years ago by Alexander Rautenberg

• ### Split 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.

83 people like this

Posted: 13 years ago by Dmitri Pavlenkov

• ### Python'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: 13 years ago by Cameron Frederick

• ### Hughes's CPSFuncList

A CPS version of FuncList, in order to avoid blowing the stack.

3 people like this

Posted: 13 years ago by Nick Palladinos

• ### ByteString

An initial attempt at creating a ByteString type based on the Haskell version.

4 people like this

Posted: 13 years ago by Ryan Riley

• ### Random Subset

A function that takes a random subset from a seq<'T>.

1 people like this

Posted: 12 years ago by Taha Hachana

• ### Remove duplicate list elements

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 this

Posted: 12 years ago by visProcessEngg

• ### Appending Two Lists Based on A Discriminated Union Type Using Continuation

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 this

Posted: 11 years ago by Joel Huang

• ### Split A Seq Into Chunks

This function splits a sequence into lists of length n until there is less than n elements then those are returned.

12 people like this

Posted: 11 years ago by Taha Hachana

• ### Maximum Sublist Problem

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 this

Posted: 11 years ago by Joel Huang

• ### An active pattern combinator for list inputs

A few active pattern combinators useful when pattern matching on lists is necessary.

2 people like this

Posted: 11 years ago by Eirik Tsarpalis

• ### intersperse last

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 this

Posted: 10 years ago by devshorts

• ### Print a list in a spiral

Prints a list in a spiral

3 people like this

Posted: 10 years ago by devshorts

• ### List Multipartition

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 this

Posted: 10 years ago by @BrockSamsonUK

• ### drop

Removes the nth element from a list

2 people like this

Posted: 9 years ago by Antonio Prestes García

• ### Compress List

Compress List

2 people like this

Posted: 9 years ago by Pitch Rock

• ### Cross Join Lists

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 this

Posted: 9 years ago by Faisal Waris

• ### BELLMAN-FORD algorithm

BELLMAN-FORD algorithm to the shortest path based on pseudo code in Algortihms Unlocked

4 people like this

Posted: 8 years ago by Fabio Galuppo

• ### Function 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.

4 people like this

Posted: 7 years ago by Tuomas Hietanen

• ### Chunk String by Size into List of String Chunks

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 this

Posted: 5 years ago by LSM07

• ### Pascal's Triangle

Creates a jagged 2d list of Pascal's Triangle.

2 people like this

Posted: 4 years ago by Kayden Tebau

• ### Coord

Playing with coordinates and moves See https://github.com/giuliohome/AdventOfCode2019/blob/master/Day15-Part1-QueueVersion.fs#L54-L66

1 people like this

Posted: 4 years ago by giuliohome

• ### phantom types for dependent types; sized list; less than

Using phantom types to emulate simple dependent types and sized list. Inspired by: https://www.seas.upenn.edu/~sweirich/cis670/09/

6 people like this

Posted: 4 years ago by Vankata

• ### QuickSort on List

Another version of QuickSort implementation.

6 people like this

Posted: 2 years ago by Shankar V

• ### Pipeline 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.

4 people like this

Posted: 4 months ago by Tomas Petricek