passing a function to a function SML - functional-programming

Below is SML code to compute a definite integral using the trapezoidal method given input f=unary function, a & b=range to take integral under, and n=number of sub-intervals to divide the range into.
fun integrate f a b n =
let val w = (b - a) / (real n)
fun genBlock c = let val BB = f c
val SB = f (c+w)
in (BB + SB) * w / 2.0
end
fun sumSlice 0 c acc = acc
| sumSlice n c acc = sumSlice (n-1) (c+w) (acc + (genBlock c))
in sumSlice n a 0.0
end
Problem is I can't figure out for the life of me how to define a function (say X cubed) and feed it to this function with a,b, and n. Here's a screenshot of me trying and receiving an error:
In this picture I define cube x =xxx and show it works, then try to feed it to the integrate function to no avail.

The error message is pretty specific: integrate is expecting a function of type real -> real but you defined a function, cube, of type int -> int.
There are a couple of things you can do:
1) Add a type annotation to the definition of cube:
- fun cube x:real = x*x*x;
val cube = fn : real -> real
And then:
- integrate cube 0.0 5.0 5;
val it = 162.5 : real
2) You can dispense with defining cube as a named function and just pass the computation as an anonymous function. In this case, SML's type inference mechanism gives the function x => x*x*x the intended type:
- integrate (fn x => x*x*x) 0.0 5.0 5;
val it = 162.5 : real

Related

several questions about this sml recursion function

When f(x-1) is called, is it calling f(x) = x+10 or f(x) = if ...
Is this a tail recursion?
How should I rewrite it using static / dynamic allocation?
let fun f(x) = x + 10
in
let fun f(x) = if x < 1 then 0 else f(x-1)
in f(3)
end
end
Before addressing your questions, here are some observations about your code:
There are two functions f, one inside the other. They're different from one another.
To lessen this confusion you can rename the inner function to g:
let fun f(x) = x + 10
in
let fun g(x) = if x < 1 then 0 else g(x-1)
in g(3)
end
end
This clears up which function calls which by the following rules: The outer f is defined inside the outer in-end, but is immediately shadowed by the inner f. So any reference to f on the right-hand side of the inner fun f(x) = if ... is shadowed because fun enables self-recursion. And any reference to f within the inner in-end is shadowed
In the following tangential example the right-hand side of an inner declaration f does not shadow the outer f if we were using val rather than fun:
let fun f(x) = if (x mod 2 = 0) then x - 10 else x + 10
in
let val f = fn x => f(x + 2) * 2
in f(3)
end
end
If the inner f is renamed to g in this second piece of code, it'd look like:
let fun f(x) = if (x mod 2 = 0) then x - 10 else x + 10
in
let val g = fn x => f(x + 2) * 2
in g(3)
end
end
The important bit is that the f(x + 2) part was not rewritten into g(x + 2) because val means that references to f are outer fs, not the f being defined, because a val is not a self-recursive definition. So any reference to an f within that definition would have to depend on it being available in the outer scope.
But the g(3) bit is rewritten because between in-end, the inner f (now g) is shadowing. So whether it's a fun or a val does not matter with respect to the shadowing of let-in-end.
(There are some more details wrt. val rec and the exact scope of a let val f = ... that I haven't elaborated on.)
As for your questions,
You should be able to answer this now. A nice way to provide the answer is 1) rename the inner function for clarity, 2) evaluate the code by hand using substitution (one rewrite per line, ~> denoting a rewrite, so I don't mean an SML operator here).
Here's an example of how it'd look with my second example (not your code):
g(3)
~> (fn x => f(x + 2) * 2)(3)
~> f(3 + 2) * 2
~> f(5) * 2
~> (if (5 mod 2 = 0) then 5 - 10 else 5 + 10) * 2
~> (if (1 = 0) then 5 - 10 else 5 + 10) * 2
~> (5 + 10) * 2
~> 15 * 2
~> 30
Your evaluation by hand would look different and possibly conclude differently.
What is tail recursion? Provide a definition and ask if your code satisfies that definition.
I'm not sure what you mean by rewriting it using static / dynamic allocation. You'll have to elaborate.

implementing an algorithm to transform a real number to a continued fraction in #F

i am trying to implement a recursive function which takes a float and returns a list of ints representing the continued fraction representation of the float (https://en.wikipedia.org/wiki/Continued_fraction) In general i think i understand how the algorithm is supposed to work. its fairly simply. What i have so far is this:
let rec float2cfrac (x : float) : int list =
let q = int x
let r = x - (float q)
if r = 0.0 then
[]
else
q :: (float2cfrac (1.0 / r ))
the problem is with the base case obviously. It seems the value r never does reduce to 0.0 instead the algorithm keeps on returning values which are the likes of 0.0.....[number]. I am just not sure how to perform the comparison. How exactly should i go about it. The algorithm the function is based on says the base case is 0, so i naturally interpret this as 0.0. I dont see any other way. Also, do note that this is for an assignment where i am explicitly asked to implement the algorithm recursively. Does anyone have some guidance for me? It would be much appreciated
It seems the value r never does reduce to 0.0 instead the algorithm keeps on returning values which are the likes of 0.0.....[number].
This is a classic issue with floating point comparisons. You need to use some epsilon tolerance value for comparisons, because r will never reach exactly 0.0:
let epsilon = 0.0000000001
let rec float2cfrac (x : float) : int list =
let q = int x
let r = x - (float q)
if r < epsilon then
[]
else
q :: (float2cfrac (1.0 / r))
> float2cfrac 4.23
val it : int list = [4; 4; 2; 1]
See this MSDN documentation for more.
You could define a helper function for this:
let withinTolerance (x: float) (y: float) e =
System.Math.Abs(x - y) < e
Also note your original solution isn't tail-recursive, so it consumes stack as it recurses and could overflow the stack. You could refactor it such that a float can be unfolded without recursion:
let float2cfrac (x: float) =
let q = int x
let r = x - (float q)
if withinTolerance r 0.0 epsilon then None
else Some (q, (1.0 / r))
4.23 |> Seq.unfold float2cfrac // seq [4; 4; 2; 1]

Creating a function that returns a curried function (SML)

I've written a function that calculates a value of x, of a polynomial made from a list of reals.
infixr 5 ^^;
fun (x:real) ^^ 0 = 1.0
| (x:real) ^^ n = x*(x^^(n-1));
fun poly [] (x:real) = 0.0
| poly (hd::tl) (x:real) = hd*(x^^(length tl)) + poly tl x;
This code all works perfectly fine, and I'm quite proud of it.
I have managed to create polynomial functions using partial application:
> fun f x = poly [3.0,2.0,1.0] x;
val f = fn : real -> real
> f 2.0;
val it = 17.0 : real
Creating the mathetmatical function:f(x) = 3*x^2 + 2*x + 1
This is all fine, but I want to be able to construct a function by this method:
fun f x = polyGen [1.0,2.0,3.0];
And it will give me an equivalent function to the one above.
Is this possible?
I know it seems trivial, I could just put an x there as I did before and get on with my life. But I'm just curious on how someone would get around this problem!
Thanks in advance, Ciaran
EDIT:
fun polyGen L = let fun poly [] x = 0.0
| poly (hd::tl) x = hd + x*(poly tl x);
in fn x => poly L x end;
Lovely!
If I understand your question correctly, then you don't need to define anything else at all. With the function poly that you have you can already do
val f = poly [3.0, 2.0, 1.0]
which defines f as a function of type real -> real.

Memoization in OCaml?

It is possible to improve "raw" Fibonacci recursive procedure
Fib[n_] := If[n < 2, n, Fib[n - 1] + Fib[n - 2]]
with
Fib[n_] := Fib[n] = If[n < 2, n, Fib[n - 1] + Fib[n - 2]]
in Wolfram Mathematica.
First version will suffer from exponential explosion while second one will not since Mathematica will see repeating function calls in expression and memoize (reuse) them.
Is it possible to do the same in OCaml?
How to improve
let rec fib n = if n<2 then n else fib (n-1) + fib (n-2);;
in the same manner?
The solution provided by rgrinberg can be generalized so that we can memoize any function. I am going to use associative lists instead of hashtables. But it does not really matter, you can easily convert all my examples to use hashtables.
First, here is a function memo which takes another function and returns its memoized version. It is what nlucaroni suggested in one of the comments:
let memo f =
let m = ref [] in
fun x ->
try
List.assoc x !m
with
Not_found ->
let y = f x in
m := (x, y) :: !m ;
y
The function memo f keeps a list m of results computed so far. When asked to compute f x it first checks m to see if f x has been computed already. If yes, it returns the result, otherwise it actually computes f x, stores the result in m, and returns it.
There is a problem with the above memo in case f is recursive. Once memo calls f to compute f x, any recursive calls made by f will not be intercepted by memo. To solve this problem we need to do two things:
In the definition of such a recursive f we need to substitute recursive calls with calls to a function "to be provided later" (this will be the memoized version of f).
In memo f we need to provide f with the promised "function which you should call when you want to make a recursive call".
This leads to the following solution:
let memo_rec f =
let m = ref [] in
let rec g x =
try
List.assoc x !m
with
Not_found ->
let y = f g x in
m := (x, y) :: !m ;
y
in
g
To demonstrate how this works, let us memoize the naive Fibonacci function. We need to write it so that it accepts an extra argument, which I will call self. This argument is what the function should use instead of recursively calling itself:
let fib self = function
0 -> 1
| 1 -> 1
| n -> self (n - 1) + self (n - 2)
Now to get the memoized fib, we compute
let fib_memoized = memo_rec fib
You are welcome to try it out to see that fib_memoized 50 returns instantly. (This is not so for memo f where f is the usual naive recursive definition.)
You pretty much do what the mathematica version does but manually:
let rec fib =
let cache = Hashtbl.create 10 in
begin fun n ->
try Hashtbl.find cache n
with Not_found -> begin
if n < 2 then n
else
let f = fib (n-1) + fib (n-2) in
Hashtbl.add cache n f; f
end
end
Here I choose a hashtable to store already computed results instead of recomputing them.
Note that you should still beware of integer overflow since we are using a normal and not a big int.

A better way to understand the logic of nested functions (Currying)

I am learning Jason Hickey's Introduction to Objective Caml. Just have a question about nested functions (currying).
there is a existing question How to understand the "Currying" in Haskell?, but I guess I am seeking for the answer of slightly different question.
It says that we can write let sum = fun i j -> i + j;; as let sum = fun i -> fun j -> i + j;;
My question is simple:
Can I understand the above definition in this way: let sum = fun i -> i + fun j -> j;;?
I know it won't pass the compiler, but I just try to map this kind of OCaml function definition to the mathematics functions.
In my above imagination, we can write the function easily in mathematics, f(i) = i + g(j); and g(j) = j.
Should I always do this kind of logic mapping for easy understanding?
Your imagination is not really correct: f(i) = i + g(j) doesn't mean much as j is undefined here.
A correct way to understand fun i j -> foo is to consider it some convenient syntactic sugar for the more explicit notation fun i -> fun j -> foo.
All the definitions below are exactly equivalent :
let sum i j = i + j
let sum i = fun j -> i + j
let sum = fun i -> (fun j -> i + j)
let sum = fun i -> fun j -> i + j
let sum = fun i j -> i + j
let sum i =
let add_i = fun j -> i + j in
add_i
Mathematically this could be written (i ↦ (j ↦ i+j)), as an element of
the function space (ℕ → (ℕ → ℕ)).
OCaml functions can be directly mapped to and from mathematical notation. However, you need to realize that mathematical notation is ambiguous. There is not a clear distinction between the value of the function, f(i), on a particular value i, and the function f itself. Often one writes f(i) when one means the function f itself. ("Let us consider a function f(i)=i+1... This function is ...") To write correct code in OCaml, you have to see clearly whether you are working with a function itself or with the value of a function.
When you say in mathematical notation, "Consider the function f(i,j)=i+g(j) where g(j)=j, you are writing the values of the functions. In OCaml, this is translated to
let f i j =
let g j = j in
i + g j;;
or
let f =
let g = fun j -> j
in
fun i j -> i + g j;;
If you are trying to write let sum = fun i -> i + fun j -> j;;, then in mathematical notation you are saying "Consider the function sum such that sum(i) = i + g, where g is a function defined by g(j)=j." This is mathematically incorrect: you cannot add an integer value i and a function g. You can only add an integer i and the value of the function g on some other integer j. The expression "i+g" is undefined, strictly speaking. Either you wanted to write i + g(i), or i+g(j), but not i+g. This is so in mathematics, and this is so in OCaml.

Resources