1 people like it.

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: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
  let equi lst =
    let rec loop acc p left right = function
      | h::t ->
        let acc = if left = right-h then p::acc else acc
        loop acc (p+1) (left+h) (right-h) t
      | [] -> acc |> List.rev
    loop [] 0 0 (lst |> List.sum) lst

  let listTest = [1;2;3;-1;0;1;-1;5;-5;5;5;15;-5]
  equi listTest
  // Result
  // val it : int list = [10]
val equi : lst:int list -> int list

Full name: Script.equi
val lst : int list
val loop : (int list -> int -> int -> int -> int list -> int list)
val acc : int list
val p : int
val left : int
val right : int
val h : int
val t : int list
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 rev : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.rev
val sum : list:'T list -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.List.sum
val listTest : int list

Full name: Script.listTest

More information

Link:http://fssnip.net/f2
Posted:11 years ago
Author:Joel Huang
Tags: equilibrium index , list