68 people like it.

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.

1: 
2: 
3: 
4: 
5: 
let lookup k = List.tryFind (fst >> ((=) k))

(* Example *)

[('a', 1); ('b', 2); ('c', 3)] |> lookup 'c'
val lookup : k:'a -> (('a * 'b) list -> ('a * 'b) option) (requires equality)

Full name: Script.lookup
val k : 'a (requires equality)
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val tryFind : predicate:('T -> bool) -> list:'T list -> 'T option

Full name: Microsoft.FSharp.Collections.List.tryFind
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/15
Posted:13 years ago
Author:Alex Muscar
Tags: list , lookup