Sort using a custom comparator: Difference between revisions

Content added Content deleted
Line 932: Line 932:
:<!wrt> List string =
:<!wrt> List string =
list_vt2t (list_mergesort_cloref<string> (lst, cmp))
list_vt2t (list_mergesort_cloref<string> (lst, cmp))

var one = 1
val p_one = addr@ one


fn
fn
Line 1,022: Line 1,019:
:<cloref> int =
:<cloref> int =
let
let
(* Merely to demonstrate this is a closure instead of a
regular function, get the value of "one" from a global
variable. *)
val @(pf, fpf | p) = $UN.ptr_vtake{int} p_one
val one = !p
prval () = fpf pf

val m = length x
val m = length x
and n = length y
and n = length y
in
in
if m < n then
if m < n then
one
1
else if n < m then
else if n < m then
~one
~1
else
else
strcasecmp (x, y)
strcasecmp (x, y)
Line 1,046: Line 1,036:
sort_by_calling_back_to_a_closure (lst : List string)
sort_by_calling_back_to_a_closure (lst : List string)
:<!wrt> List string =
:<!wrt> List string =
(* Incorporate a closure into the sort implementation. (Standard C
does not have closures.) *)
let
let
fn
fn
Line 1,054: Line 1,042:
:<cloref> int =
:<cloref> int =
let
let
(* Merely to demonstrate this is a closure instead of a
regular function, get the value of "one" from a global
variable. *)
val @(pf, fpf | p) = $UN.ptr_vtake{int} p_one
val one = !p
prval () = fpf pf

val m = length x
val m = length x
and n = length y
and n = length y
in
in
if m < n then
if m < n then
one
1
else if n < m then
else if n < m then
~one
~1
else
else
strcasecmp (x, y)
strcasecmp (x, y)