0 people like it.

generating partions of a number

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
let rec part = function
  | 1 -> [[1]]
  | n -> [yield [n]
          for i in 1..(n-1) do
            for ls in (part (n-i)) do
              if (List.head ls) <= i then
                yield i::ls]

let rec part' = function
  | 1 -> [[1]]
  | n -> [yield [n]
          for i in 1..(n-1) do
            for ls in (part' (n-i)) do
              ///if (List.head ls) <= i then
                yield i::ls]
  
List.length <| part 5
List.length <| part' 5
val part : _arg1:int -> int list list

Full name: Script.part
val n : int
val i : int
val ls : 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 head : list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.head
val part' : _arg1:int -> int list list

Full name: Script.part'
val length : list:'T list -> int

Full name: Microsoft.FSharp.Collections.List.length
Raw view Test code New version

More information

Link:http://fssnip.net/aC
Posted:14 years ago
Author:
Tags: