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 res ch num =
        match num with
        | 0 -> res
        | _ -> ch::replicateInner res 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 list -> 'b -> int -> 'b list)
val res : '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
Next Version Raw view Test code New version

More information

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