0 people like it.

"99 OCaml" #3

Find the k'th element of a list. (easy)

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
let rec at i = function
    | h :: t when 1 < i -> at (i - 1) t
    | h :: t when 1 = i -> Some h
    | _ -> None

at 2 [ "a" ; "b" ; "c" ; "d" ]
at -1 [ "a" ; "b" ; "c" ; "d" ]
at 2 [ "a" ]
at 2 ([ ] : int list)
val at : i:int -> _arg1:'a list -> 'a option

Full name: Script.at
val i : int
val h : 'a
val t : 'a list
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
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/rh
Posted:9 years ago
Author:DC F#
Tags: ocaml