Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Function-composition/ATS/function-composition-1.ats
Normal file
46
Task/Function-composition/ATS/function-composition-1.ats
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(*
|
||||
The task:
|
||||
|
||||
Create a function, compose, whose two arguments f and g, are
|
||||
both functions with one argument.
|
||||
|
||||
The result of compose is to be a function of one argument,
|
||||
(let's call the argument x), which works like applying function
|
||||
f to the result of applying function g to x.
|
||||
|
||||
In ATS, we have to choose whether to use non-linear closures
|
||||
(cloref) or linear closures (cloptr). In the latter case, we also
|
||||
have to choose between closures allocated with malloc (or similar)
|
||||
and closures allocated on the stack.
|
||||
|
||||
For simplicity, we will use non-linear closures and assume there is
|
||||
a garbage collector, or that the memory allocated for the closures
|
||||
can be allowed to leak. (This is often the case in a program that
|
||||
does not run continuously.)
|
||||
*)
|
||||
|
||||
#include "share/atspre_staload.hats"
|
||||
|
||||
(* The following is actually a *template function*, rather than a
|
||||
function proper. It is expanded during template processing. *)
|
||||
|
||||
fn {t1, t2, t3 : t@ype}
|
||||
compose (f : t2 -<cloref1> t3,
|
||||
g : t1 -<cloref1> t2) : t1 -<cloref1> t3 =
|
||||
lam x => f (g (x))
|
||||
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val one_hundred = 100.0
|
||||
val char_zero = '0'
|
||||
val f = (lam y =<cloref1> add_double_int (one_hundred, y))
|
||||
val g = (lam x =<cloref1> char2i x - char2i char_zero)
|
||||
val z = compose (f, g) ('5')
|
||||
val fg = compose (f, g)
|
||||
val w = fg ('7')
|
||||
in
|
||||
println! (z : double);
|
||||
println! (w : double)
|
||||
end
|
||||
28
Task/Function-composition/ATS/function-composition-2.ats
Normal file
28
Task/Function-composition/ATS/function-composition-2.ats
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
fn {t1, t2, t3 : t@ype}
|
||||
compose (f : t2 -<cloref1> t3,
|
||||
g : t1 -<cloref1> t2) : t1 -<cloref1> t3 =
|
||||
lam x => f (g (x))
|
||||
|
||||
fn
|
||||
compose_char2int2double
|
||||
(f : int -<cloref1> double,
|
||||
g : char -<cloref1> int) :
|
||||
char -<cloref1> double =
|
||||
compose<char, int, double> (f, g)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val one_hundred = 100.0
|
||||
val char_zero = '0'
|
||||
val f = (lam y =<cloref1> add_double_int (one_hundred, y))
|
||||
val g = (lam x =<cloref1> char2i x - char2i char_zero)
|
||||
val z = compose_char2int2double (f, g) ('5')
|
||||
val fg = compose_char2int2double (f, g)
|
||||
val w = fg ('7')
|
||||
in
|
||||
println! (z : double);
|
||||
println! (w : double)
|
||||
end
|
||||
29
Task/Function-composition/ATS/function-composition-3.ats
Normal file
29
Task/Function-composition/ATS/function-composition-3.ats
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include "share/atspre_staload.hats"
|
||||
|
||||
fn {t1, t2, t3 : t@ype}
|
||||
compose (f : t2 -<cloref1> t3,
|
||||
g : t1 -<cloref1> t2) :<cloref1>
|
||||
t1 -<cloref1> t3 =
|
||||
lam x => f (g (x))
|
||||
|
||||
fn
|
||||
compose_char2int2double
|
||||
(f : int -<cloref1> double,
|
||||
g : char -<cloref1> int) :<cloref1>
|
||||
char -<cloref1> double =
|
||||
compose<char, int, double> (f, g)
|
||||
|
||||
implement
|
||||
main0 () =
|
||||
let
|
||||
val one_hundred = 100.0
|
||||
val char_zero = '0'
|
||||
val f = (lam y =<cloref1> add_double_int (one_hundred, y))
|
||||
val g = (lam x =<cloref1> char2i x - char2i char_zero)
|
||||
val z = compose_char2int2double (f, g) ('5')
|
||||
val fg = compose_char2int2double (f, g)
|
||||
val w = fg ('7')
|
||||
in
|
||||
println! (z : double);
|
||||
println! (w : double)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue