Why the same function prints different output? - functional-programming

I have defined the following module to implement a matrix type:
module MatrixImplementation: MatrixADT.MatrixInterface =
struct
type 'a matrix = {n: int; m: int; c: 'a array array};;
let zeroes n m = {n= n; m= m; c= Array.make_matrix n m 0};;
let identity n =
let m = zeroes n n
in for i = 0 to (n-1) do
m.c.(i).(i) <- 1
done;
(m);
;;
let init n =
let m = zeroes n n
in for i = 0 to (n-1)do
for j = 0 to (n-1) do
m.c.(i).(j) <- (n-1) * i + j;
done;
done;
(m);
;;
(* . . . *)
let rec print_row rl =
match rl with
| [] -> print_string("");
| v::cl -> Format.printf "%2d " v; print_row cl;
;;
let rec print_matrix m =
match Array.to_list m.c with
| [] -> print_string("");
| r::rl ->
print_string "[ ";
print_row (Array.to_list r);
print_string "]\n";
print_matrix {n= ((m.n)-1); m= (m.m); c= Array.of_list rl};
;;
end;;
However when I declare matrices using the module functions (zeroes, identity, init) and try to print them, only part of the lines that compose them are formatted, the first lines are not correctly formatted.
For instance i tried with:
let empty = zeroes 3 5;;
let id = identity 4;;
let mat = init 5;;
print_matrix mat;;
print_matrix empty;;
print_matrix id;;
And I got it as a result:
[ ]
[ ]
[ ]
[ ]
[ ]
[ 0 1 2 3 4 4 5 6 7 8 8 9 10 11 12 12 13 14 15 16 16 17 18 19 20 0 0 0 0 0 ]
[ 0 0 0 0 0 ]
[ 0 0 0 0 0 ]
[ 1 0 0 0 ]
[ 0 1 0 0 ]
[ 0 0 1 0 ]
[ 0 0 0 1 ]

Stripping your code of unnecessary ; and ;; tokens, we can also implement your print_matrix function imperatively. Given that you're using arrays, this simply makes more sense.
Converting from arrays to lists and then back so that you can use recursion and pattern matching is both inefficient and makes your code more difficult to understand.
module MatrixImplementation =
struct
type 'a matrix = {n : int; m : int; c : 'a array array}
let zeroes n m = {n = n; m = m; c = Array.make_matrix n m 0}
let identity n =
let m = zeroes n n in
for i = 0 to (n-1) do
m.c.(i).(i) <- 1
done;
m
let init n =
let m = zeroes n n in
for i = 0 to (n-1) do
for j = 0 to (n-1) do
m.c.(i).(j) <- (n-1) * i + j
done;
done;
m
let print_matrix {n; m; c} =
for i = 0 to n - 1 do
print_string "[";
for j = 0 to m - 1 do
Printf.printf " %d " c.(i).(j)
done;
print_string "]\n"
done
end
utop # MatrixImplementation.(init 3 |> print_matrix);;
[ 0 1 2 ]
[ 2 3 4 ]
[ 4 5 6 ]
- : unit = ()
A note: print_string "" effectively does nothing. If you need to actually do nothing, you can simply return () (unit).
Note also that in print_matrix I've used pattern matching right in the function arguments (let print_matrix {n; m; c} =) to save myself some time. This could have been written as:
let print_matrix m =
for i = 0 to m.n - 1 do
print_string "[";
for j = 0 to m.m - 1 do
Printf.printf " %d " m.c.(i).(j)
done;
print_string "]\n"
done
We could take advantage of the Array.iter function.
let print_matrix {n; m; c} =
Array.(
let print_row arr =
print_string "[";
arr |> iter (Printf.printf " %d ");
print_string "]\n"
in
c |> iter print_row
)
If we consider your original approach, the following will fix it.
let rec print_row rl =
List.iter (Printf.printf " %2d ") rl

Reviewing the code of my module and its incorrect behavior, the problem is not the way the print functions are defined (leaving out the tricks suggested by #Chris in his answer), but the module used: the Format module should be used to make more complex printouts (API reference), the formatting error is probably due to the arguments of the fmt string; for simpler printing, the Printf module and its printf function (same arguments) are more suitable.
Therefore a possible change to the printing functions is as follows:
let rec print_row = function
| [] -> ()
| c::cl -> Printf.printf "%2d " c; print_row cl;
and print_matrix m =
match m.c with
| [] -> ()
| rows ->
for i= 0 to (List.length rows)-1 do
print_string("[ ");
print_row (List.nth rows i);
print_string("]\n");
done;
;;
(In this case the field c (content) of type definition matrix is int list list to avoid conversions)

Related

Is there any way to optimize this function and make it faster?

is there a way to optimize this? It is taking way too long to run
let counter2 = ref 0
let rec s2 num =
counter2 := !counter2 + 1;
match num with
| 0 -> 1
| 1 -> 2
| _ -> (((((6*num)-3) * (s2 (num-1))) / (num+1))) - (((num-2)* (s2 (num-2))/(num+1)))
Here is the highly recursive definition of the Fibonacci sequence:
let rec fib n =
if n < 2 then n
else fib (n - 2) + fib (n - 1)
Here is the not so recursive definition of the Fibonacci sequence.
let nfib n =
let rec helper pprev prev i =
if i = n then
pprev + prev
else
helper prev (pprev + prev) (i + 1)
in
if n < 2 then n
else helper 0 1 2
Here is a function for timing things:
let time f x =
let st = Unix.gettimeofday () in
let res = f x in
Printf.printf "%f seconds\n" (Unix.gettimeofday () -. st);
res
Here are times for the fib and nfib functions:
# time fib 42;;
7.694294 seconds
- : int = 267914296
# time nfib 42;;
0.000002 seconds
- : int = 267914296

For Loop Over a Recursive Call Ocaml

I'm working on an implementation of prime decomposition in OCaml. I am not a functional programmer; Below is my code. The prime decomposition happens recursively in the prime_part function. primes is the list of primes from 0 to num. The goal here being that I could type prime_part into the OCaml interpreter and have it spit out when n = 20, k = 1.
2 + 3 + 7
5 + 7
I adapted is_prime and all_primes from an OCaml tutorial. all_primes will need to be called to generate a list of primes up to b prior to prime_part being called.
(* adapted from http://www.ocaml.org/learn/tutorials/99problems.html *)
let is_prime n =
let n = abs n in
let rec is_not_divisor d =
d * d > n || (n mod d <> 0 && is_not_divisor (d+1)) in
n <> 1 && is_not_divisor 2;;
let rec all_primes a b =
if a > b then [] else
let rest = all_primes (a + 1) b in
if is_prime a then a :: rest else rest;;
let f elem =
Printf.printf "%d + " elem
let rec prime_part n k lst primes =
let h elem =
if elem > k then
append_item lst elem;
prime_part (n-elem) elem lst primes in
if n == 0 then begin
List.iter f lst;
Printf.printf "\n";
()
end
else
if n <= k then
()
else
List.iter h primes;
();;
let main num =
prime_part num 1 [] (all_primes 2 num)
I'm largely confused with the reclusive nature with the for loop. I see that List.ittr is the OCaml way, but I lose access to my variables if I define another function for List.ittr. I need access to those variables to recursively call prime_part. What is a better way of doing this?
I can articulate in Ruby what I'm trying to accomplish with OCaml. n = any number, k = 1, lst = [], primes = a list of prime number 0 to n
def prime_part_constructive(n, k, lst, primes)
if n == 0
print(lst.join(' + '))
puts()
end
if n <= k
return
end
primes.each{ |i|
next if i <= k
prime_part_constructive(n - i, i, lst+[i], primes)
}
end
Here are a few comments on your code.
You can define nested functions in OCaml. Nested functions have access to all previously defined names. So you can use List.iter without losing access to your local variables.
I don't see any reason that your function prime_part_constructive returns an integer value. It would be more idiomatic in OCaml for it to return the value (), known as "unit". This is the value returned by functions that are called for their side effects (such as printing values).
The notation a.(i) is for accessing arrays, not lists. Lists and arrays are not the same in OCaml. If you replace your for with List.iter you won't have to worry about this.
To concatenate two lists, use the # operator. The notation lst.concat doesn't make sense in OCaml.
Update
Here's how it looks to have a nested function. This made up function takes a number n and a list of ints, then writes out the value of each element of the list multiplied by n.
let write_mults n lst =
let write1 m = Printf.printf " %d" (m * n) in
List.iter write1 lst
The write1 function is a nested function. Note that it has access to the value of n.
Update 2
Here's what I got when I wrote up the function:
let prime_part n primes =
let rec go residue k lst accum =
if residue < 0 then
accum
else if residue = 0 then
lst :: accum
else
let f a p =
if p <= k then a
else go (residue - p) p (p :: lst) a
in
List.fold_left f accum primes
in
go n 1 [] []
It works for your example:
val prime_part : int -> int list -> int list list = <fun>
# prime_part 12 [2;3;5;7;11];;
- : int list list = [[7; 5]; [7; 3; 2]]
Note that this function returns the list of partitions. This is much more useful (and functional) than writing them out (IMHO).

Ocaml nested if without else

Is it possible to have nested if without else statements. I wrote the following useless program to demonstrate nested ifs. How do I fix this so it's correct in terms of syntax. lines 5 and 6 gives errors.
let rec move_helper b sz r = match b with
[] -> r
|(h :: t) ->
if h = 0 then
if h - 1 = sz then h - 1 ::r
if h + 1 = sz then h + 1 ::r
else move_helper t sz r
;;
let move_pos b =
move_helper b 3 r
;;
let g = move_pos [0;8;7;6;5;4;3;2;1]
You can't have if without else unless the result of the expression is of type unit. This isn't the case for your code, so it's not possible.
Here's an example where the result is unit:
let f x =
if x land 1 <> 0 then print_string "1";
if x land 2 <> 0 then print_string "2";
if x land 4 <> 0 then print_string "4"
You must understand that if ... then is an expression like any other. If no else is present, it must be understood as if ... then ... else () and thus has type unit. To emphasize the fact that it is an expression, suppose you have two functions f and g of type, say, int → int. You can write
(if test then f else g) 1
You must also understand that x :: r does not change r at all, it constructs a new list putting x in front of r (the tail of this list is shared with the list r). In your case, the logic is not clear: what is the result when h=0 but the two if fail?
let rec move_helper b sz r = match b with
| [] -> r
| h :: t ->
if h = 0 then
if h - 1 = sz then (h - 1) :: r
else if h + 1 = sz then (h + 1) :: r
else (* What do you want to return here? *)
else move_helper t sz r
When you have a if, always put an else. Because when you don't put an else, Java will not know if the case is true or false.

reverse recursion in F#

(I am not sure about that whether reverse recursion is a correct name..)
I'd like to define a recursive function with initial value at k=n and terminate at k = 0
So I tried to write it in this way(here n = 10):
let rec f k =
match k with
|_ when k > 10 -> 0
| 10 -> 1
|_ -> (f n+1)-1
and the VS crashed. Could anyone help me?
dividing line------------------
I think my question is a little like this:
Consider a sequence with
a(10) = 1
a(k) = [a(k+1)*a(k+1)] + 1
a(k) = 0 if (k < 0 or k > 10)
How could I implement it in the F# ?
let rec f k =
match k with
|_ when k > 10 || k < 0 -> 0
| 10 -> 1
|_ -> f (k+1) * f (k+1) + 1

Why ocamlc says I mismatched {} while I don't?

I have written myPercolation.ml.
open MyUnionFind
module type MyPercolationSig = sig
type percolation
val create_percolation : int -> percolation
val open_site : percolation -> int -> int -> unit
val is_open : percolation -> int -> int -> bool
val is_full : percolation -> int -> int -> bool
val can_percolates : percolation -> bool
end
module MyPercolation : MyPercolationSig = struct
exception IndexOutOfBounds;;
type percolation =
{n : int;
sites: bool array;
union : MyUnionFind.union_find};;
let create_percolation n =
{n = n; sites = Array.make (n*n) false; union = MyUnionFind.create_union (n*n)};;
let open_site p i j =
let {n;_;union} = p
in
if not (is_open p i j) then
begin
sites.(index_of n i j) <- true;
if i - 1 >= 1 && i - 1 <= n && is_open n (i-1) j then
MyUnionFind.union union (index_of n i j) (index_of n (i-1) j)
else if i + 1 >= 1 && i + 1 <= n && is_open n (i+1) j then
MyUnionFind.union union (index_of n i j) (index_of n (i+1) j)
else if j - 1 >= 1 && j - 1 <= n && is_open n i (j-1) then
MyUnionFind.union union (index_of n i j) (index_of n i (j-1))
else if j + 1 >= 1 && j + 1 <= n && is_open n i (j+1) then
MyUnionFind.union union (index_of n i j) (index_of n i (j+1))
end;;
let index_of n i j = n * (i - 1) + j;;
let is_open {n;sites;_} i j =
if i < 1 || i > n || j < 1 || j > n then
raise IndexOutOfBounds
else
sites.(index_of n i j);;
let is_full {n;_;union} i j =
let rec is_connected_top j' =
if j = 0 then false
else
if MyUnionFind.is_connected union (index_of n i j) (index_of n 0 j') then true
else is_connected_top (j'-1)
in is_connected_top n;;
let can_percolates p =
let {n;_;_} = p
in
let rec is_full_bottom j =
if j = 0 then false
else
if is_full p n j then true
else is_full_bottom (j-1)
end
Please ignore the package MyUnionFind package. It is just a homemade implementation for union-find algorithm.
when I try to compile the myPercolation.ml, I got such an error:
$ ocamlc -c myPercolation.ml
File "myPercolation.ml", line 25, characters 11-12:
Error: Syntax error: '}' expected
File "myPercolation.ml", line 25, characters 8-9:
Error: This '{' might be unmatched
I think the error is talking about let {n;_;union} = p in function of let open_site p i j.
I have read through that line and all code many times, but I still don't see any mismatched {} in that line.
can anyone help please?
Another possible error: {n;_;_} should be {n;_} Only 1 underscore is necessary. Think of it like the _ wildcard in a match statement.
The expression let {n; _; union} = p is not well formed OCaml. I think what you want is let {n; union} = p. The way to handle fields you don't care about in a record pattern is not to mention them.
Update:
As rgrinberg points out, a much better way to describe the problem is that the _ has to appear as the last field. That's why the compiler is expecting to see } afterward. It might be good style to include the _ as an indicator that you're purposely matching only a subset of the fields of the record. You can, in fact, turn on a compiler option that checks for this.
Update 2:
The warning for incomplete record patterns is warning number 9, and also is associated with the letter R. Here's how to use R:
$ ocaml -w +R
OCaml version 4.00.0
# type r = { a: int; b: char };;
type r = { a : int; b : char; }
# let {a} = {a=3; b='x'} in a;;
Warning 9: the following labels are not bound in this record pattern:
b
Either bind these labels explicitly or add '; _' to the pattern.
- : int = 3
The command-line syntax is the same for the compiler.

Resources