0 people like it.

"99 OCaml" #1

Write a function last : 'a list -> 'a option that returns the last element of a list.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let rec last = function
    | [ h ] -> Some h
    | h :: t -> last t
    | [] -> None

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

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