0 people like it.

"99 OCaml" #4

f# has Seq.length but we ask that you reimplement it. Bonus for a tail recursive solution.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
let length l = 
    let rec length' c = function
        | h :: t -> length' (c + 1) t
        | [] -> 0
    length' 0 l

length [ "a" ; "b" ; "c" ; "d" ]
length [ "a" ; "b" ; "c" ; "d" ]
length [ "a" ]
length ([ ] : int list)
val length : l:'a list -> int

Full name: Script.length
val l : 'a list
val length' : (int -> 'b list -> int)
val c : int
val h : 'b
val t : 'b list
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Raw view Test code New version

More information

Link:http://fssnip.net/ri
Posted:8 years ago
Author:DC F#
Tags: ocaml