Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.54 KB | None | 0 0
  1.  
  2.  
  3. // Indicates the namespace that contains the AlgebraicList module.
  4. namespace FunctionalCollections
  5.  
  6. // Defines the EmptyList exception.
  7. exception EmptyList of string
  8.  
  9. // Declares the AlgebraicList module.
  10. module AlgebraicList =
  11.     // Declares the alg_list algebraic data type.
  12.     type 'a alg_list = Empty | Cell of 'a * 'a alg_list
  13.  
  14.    // Returns a new empty list.
  15.    let empty = Empty
  16.  
  17.    // Adds an element at the front of a list.
  18.    let addHead elem lst = Cell (elem, lst)
  19.                        
  20.    // Returns the first element of the given list.
  21.    // Raises EmptyList exception if needed.
  22.    let head = function
  23.        | Empty -> raise (EmptyList "list is empty")
  24.        | Cell (hd,_) -> hd
  25.  
  26.    // Returns the list that remains after removing the head of the given list.
  27.    // Raises EmptyList exception if needed.
  28.    let tail = function
  29.        | Empty -> raise (EmptyList "list is empty")
  30.        | Cell (_,tl) -> tl
  31.  
  32.    // Determines the number of elements in the list.
  33.    let rec length = function
  34.        | Empty -> 0
  35.        | Cell (_,tl) -> 1 + length tl
  36.  
  37.    // Determines whether a list is empty.
  38.    let isEmpty = function
  39.        | Empty -> true
  40.        | _ -> false
  41.  
  42.    // Returns true if the element is a member of a list.
  43.    let rec isMember elem = function
  44.        | Empty -> false
  45.        | Cell(top, rest) when top = elem -> true
  46.        | Cell(_, rest) -> isMember elem rest
  47.  
  48.    // Concatenates a list with another list.
  49.    let rec append node1 node2 =
  50.        match node1 with
  51.        | Empty -> node2
  52.        | Cell(top,rest) -> addHead top (append rest node2)
  53.  
  54.    // Returns a new list with the elements of the given list in reverse order.
  55.    let rev lst =
  56.        let rec rev_helper lst lstRev =
  57.            match lst with
  58.            | Empty -> lstRev
  59.            | Cell(top, rest) -> rev_helper rest (addHead top lstRev)
  60.        rev_helper lst empty
  61.  
  62.    // Removes the first ocurrence of an element from a list.
  63.    // Raises EmptyList exception if needed.
  64.    let remove elem lst =
  65.        let rec remove_helper elem tmpList = function
  66.            | Empty -> raise (EmptyList "List is Empty")
  67.            | Cell(top, rest) when elem = top ->  append (rev tmpList) rest
  68.            | Cell(top, rest) -> remove_helper elem (addHead top tmpList) rest
  69.        remove_helper elem empty lst
  70.  
  71.    // Iterates through a list using a visit function.
  72.    let rec iter visit = function
  73.         | Empty -> ()
  74.         | Cell(hd,tl) -> visit hd
  75.                          iter visit tl
  76.  
  77.    // Creates a list by calling a generator on each index.
  78.    let init num gen =
  79.        let rec init_helper acc num gen =
  80.            match num with
  81.            | 0 -> acc
  82.            | _ -> init_helper (addHead (gen (num - 1)) acc) (num - 1) gen
  83.        init_helper empty num gen
  84.  
  85.    // Applies a function to each element of a list, accumulating a result
  86.    // based on an initial value.
  87.    let rec fold fn acc = function
  88.        | Empty -> acc
  89.        | Cell(hd,tl) -> fold fn (fn acc hd) tl
  90.  
  91.    // Applies a function to each element of a list, creating a new list.
  92.    let rec map fn = function
  93.        | Empty -> Empty
  94.        | Cell(hd,tl) -> Cell( (fn hd),(map fn tl) )
  95.  
  96.    // Creates a new list with those elements of the original for which a
  97.    // predicate is satisfied.
  98.    let rec filter pred = function
  99.        | Empty -> Empty
  100.        | Cell(hd,tl) -> if pred hd
  101.                         then Cell(hd,(filter pred tl))
  102.                         else filter pred tl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement