0 people like it.

"99 OCaml" #2

Find the last but one (last and penultimate) elements of a list. (easy)

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let rec last2 = function
    | nl :: l :: [] -> Some ( nl , l )
    | h :: t -> last2 t
    | [] -> None

last2 [ "a" ; "b" ; "c" ; "d" ]
last2 [ "a" ]
last2 ([ ] : int list)
val last2 : _arg1:'a list -> ('a * 'a) option

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