Snippets tagged list

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

    70 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: 12 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: 12 years ago by Nick Palladinos

  • String explode and implode

    Convert string to and from character lists.

    11 people like this

    Posted: 12 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: 12 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: 11 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: 10 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: 9 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: 8 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: 2 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: 1 year ago by Abel Braaksma