1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
//usr/bin/env myatscc "$0"; exit
(* ****** ****** *)
//
// HX-2016-10-17:
// For answering
// a question on StackOverflow
//
(* ****** ****** *)
//
(*
##myatsccdef=\
patsopt --constraint-ignore --dynamic $1 | \
tcc -run -DATS_MEMALLOC_LIBC -I${PATSHOME} -I${PATSHOME}/ccomp/runtime -
*)
//
(* ****** ****** *)
#include
"share/atspre_staload.hats"
(* ****** ****** *)
(*
fun
intsqrt(n: int): int =
if
(n >= 1)
then let
val n4 = n / 4
val res = 2 * intsqrt(n4) + 1
in
if (res * res <= n) then res else 2*intsqrt(n4)
end // end of [then]
else (n)
*)
(* ****** ****** *)
//
// HX:
// Every allocated byte is freed.
// Please try to valgrind to verify it!
//
(* ****** ****** *)
fun
square(x: int): int = x * x
fun
applin
(
k: int -<lincloptr1> int, x: int
) : int = res where
{
val res = k(x)
val ((*freed*)) = cloptr_free($UNSAFE.castvwtp0(k))
} (* end of [applin] *)
fun
intsqrt_cps
(
n: int, k: int -<lincloptr1> int
) : int =
if
(n >= 1)
then let
val n4 = n / 4
in
//
intsqrt_cps
( n4
, llam(res) =>
applin(k, if square(2*res+1) <= n then 2*res+1 else 2*res)
) (* intsqrt_cps *)
//
end // end of [then]
else applin(k, 0) // end of [else]
fun intsqrt(n:int): int = intsqrt_cps(n, llam(x) => x)
(* ****** ****** *)
implement
main0() =
{
val () = println! ("intsqrt(1023) = ", intsqrt(1023))
val () = println! ("intsqrt(1024) = ", intsqrt(1024))
} (* end of [main0] *)
(* ****** ****** *)
(* end of [intsqrt_cps.dats] *)
|