Skip to content

ML Problem Set 1 #1

@YLGH

Description

@YLGH

fun area (x,y) = x*y/2.0;

area(3.0,4.0);

1.0 - 0.9 - 0.1;

fun rootplus (a,b,c) = (~b + Math.sqrt(b_b-4.0_a_c))/(2.0_a);

fun eval(a,b,c,x: real) = a_x_x + b*x + c;

val x1 = rootplus(1.0,121.0,11.0);

eval(1.0,121.0,11.0, x1);

val x2 = rootplus(1.22,3.34,2.28);

eval(1.22,3.34,2.28, x2);

fun fact n =
if n = 0 then 1
else n * fact(n-1);

fact(6);

fun interfact (n,total) =
if n = 0 then total
else interfact(n-1, total*n);

interfact(6,1);
(fact(~6);)
(interfact(~6,1);)
(continued)
fun even(n) =
n mod 2 = 0;

fun power2(x,n) =
if n = 1 then x
else if even(n) then power2(x_x,n div 2)
else x_power2(x*x, n div 2): real;

1.0/power2(2.0,2);

fun sumt(n) =
if n = 1 then 1.0
else 1.0/power2(2.0, n-1) + sumt(n-1);

sumt(1);

fun sum2t(x,n) =
if n = 0 then x
else x + sum2t(x/2.0, n-1);

sum2t(2.0, 50);

fun eapprox(n) =
if n = 1 then 1.0
else 1.0/(real(fact(n-1))) + eapprox(n-1); (* ask about more efficient*)

eapprox(10);

fun exp(z,n) =
if n = 0 then 1.0
else power2(z, n-1)/(real(fact(n-1))) + exp(z, n-1);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions