0 people like it.

Replicate the elements of a list a given number of times

One of the 99 OCaml problems rewritten in F#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
let replicate list num =
    let rec replicateInner ch num =
        match num with
        | 0 -> []
        | _ -> ch::replicateInner ch (num-1)

    let rec replicateOuter list num =
        match list with
        | [] -> []
        | h::t -> (replicateInner h num) @ replicateOuter t num

    replicateOuter list num
val replicate : list:'a list -> num:int -> 'a list

Full name: Script.replicate
Multiple items
val list : 'a list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val num : int
val replicateInner : ('b -> int -> 'b list)
val ch : 'b
val replicateOuter : ('b list -> int -> 'b list)
Multiple items
val list : 'b list

--------------------
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val h : 'b
val t : 'b list

More information

Link:http://fssnip.net/rj
Posted:2 years ago
Author:Vladimir Khorikov
Tags: ocaml , f#