How to pattern match on input tuples ocaml - functional-programming

For example, I have the code
let add_next (data: int * int * int list) : int =
However,the word data is really ambiguous, and I'd like to be able to name the first two integers and then the list in the function header, while preserving the type of int * int * int list. How can this be done?

OCaml version 4.01.0
# let add_next ((first, second, l): int * int * int list) : int = first;;
val add_next : int * int * int list -> int = <fun>
If you need to pass the data tuple around without having to rebuild it, use the as construct:
# let add_next ((first, second, l) as data: int * int * int list) : int =
ignore data;
first;;
val add_next : int * int * int list -> int = <fun>

Related

Subtrees of height h in Binary tree

How do we find number of subtrees of height 'h' in a binary tree.
Function is defined as
int subtree( node *root, int k);
where k is the specific height.
First, we recursively calculate the height of the tree as follows:
If the tree is empty, the height is 0.
If the tree is non-empty, the height is the maximum height of its children, plus 1.
In C (I assume OP is using C based on response), this looks like
typedef struct Node {
Node* leftChild,
node* rightChild
} Node;
typedef Node* Tree;
unsigned int max(unsigned int a, unsigned int b) {
return a > b ? a : b;
}
unsigned int height(Tree tree) {
return tree ? 1 + max(height(tree->leftChild, tree->rightChild)) : 0;
}
Note that generally, Node will have some additional data. But that data isn't relevant here, so we don't include it (although it's easy enough to do so if you wish to).
Now, we want to modify the height function slightly. To do this, we define
typdef struct Result {
unsigned int height,
unsigned int count
} Result;
/**
* The returned .height should be the height of the tree.
* The returned .count should be the number of subtrees of tree
* with height k.
*/
Result resultCountChildren(Tree tree, unsigned int k) {
if (tree) {
Result leftResult = resultCountChildren(tree->left, k);
Result rightResult = resultCountChildren(tree->right, k);
unsigned int heightOfTree = 1 + max(leftResult.height, rightResult.height);
unsigned int count = leftResult.count + rightResult.count + (heightOfTree == k);
Result result = {
.height = heightOfTree,
.count = count
};
return result;
} else {
unsigned int height = 0;
unsigned int count = (0 == k);
Result result = {
.height = height,
.count = count
};
return result;
}
}
unsigned int count(Tree tree, unsigned int k) {
return resultCountChildren(tree).count;
}

I don't understand initialization of pointer

int main ()
{
int num_1 = 111;
int *p = &num_1;
int &ref1 = *p;
int *(&ref2) = p;
printf("&ref2 : %d\n", &ref2);
printf("*ref2 : %d\n", *ref2);
printf("ref2 : %d\n", ref2);
return 0;
}
I get pointer literally points address.
So in int &ref1 = *p; ref1's address is equal to num_1's address, hence have the same value as num1 which is 111.
However, what I don't understand is ref2 part.
If int *(&ref2) is the value of ref2 variable, shouldn't it have the same address of the num_1?
If int *(&ref2) is the value of ref2 variable, shouldn't it have the same address of the num_1?
The declaration int *(&ref2) = p; defines ref2 to be a reference to p.
Part of your misunderstanding may come from wrong output you get from using inappropriate printf conversion specifiers; it's wrong to print addresses with %d - in your first and third printf use %p instead. It might become clearer if you add
printf("p : %p\n", p);

SML: Determining Type of Function

Suppose all I know about a function is that it is of type:
int list -> int * string -> int
Is there any way of knowing in advance whether this means:
(int list -> int * string) -> int or int list -> (int * string -> int)?
Thanks,
bclayman
-> is right associative in SML type annotations, so int list -> (int * string -> int) is correct.
Consider this simple experiment in the REPL:
- fun add x y = x+y;
val add = fn : int -> int -> int
add is a function which, when fed an int, returns a function, namely the function which sends y to x + y -- hence its type is int -> (int ->int). It isn't a function which, when a fed a function from ints to ints outputs an int (which is what (int -> int) -> int would be). A somewhat artificial example of the later sort of thing is:
- fun apply_to_zero_and_increment f = 1 + f(0);
val apply_to_zero_and_increment = fn : (int -> int) -> int
If I define fun g(x) = x + 5 then apply_to_zero_and_increment g returns 6.

How to use map in a function

The function in map is pretty easy. I want to double every element in a list which can be done:
map(fn x => x * 2);
But what if I want to name this function double?
fun double = map(fn x => x * 2);
Calling this function I get
- double [1,2,3];
val it = fn : int list -> int list
How can I name this function double?
The result of map (fn x => x * 2) is a function, which can be bound to an identifier:
- val double = map (fn x => x * 2);
val double = fn : int list -> int list
- double [1,2,3];
val it = [2,4,6] : int list
The fun form is just syntactic sugar. For example:
fun name param = ...
will be desugared to:
val rec name = fn param => ...
The rec part is a keyword that lets you implement recursive function definitions.

Recursive functions - argument value not updating after some value

I have written one recursive function and pointer to integer is passed as an argument. That integer value is incremented in function, but I am facing a strange issue that after some value its value never get updated. Even I am checking the value at that address.
Below is the code:--
computeWait(long long int begin, long long int begin2, long long int w,
int* current, int limit)
{
long long int next = 0, arrival = 0;
long long int next1 = 0, service = 0;
long long int serviceTime = 0;
long long int wait = 0;
static long long int Ta = 0;
static long long int Ts = 0;
static long long int W = 0;
while(*current < limit)
{
next = (16807 * begin) % m;
arrival = -200 * log((double)next/m);
next1 = (16807 * begin2) % m;
service = -100 * log(EDRN((double)next1));
wait = max(0, (w + service - arrival));
Ta = Ta + arrival;
Ts = Ts + service;
W = W + wait;
*current = *current + 1;;
computeWait(next, next1, wait, current, limit);
}
printf("\n\nTotal arrival %Ld Total service %Ld Total wait %Ld\n", Ta/limit, Ts/limit, W/limit);
}
int main(int agrc, char* argv[])
{
int num = 0;
int currentValue = 0; // seed number
int end = 1000000;
computeWait(1, 46831694, 0, &currentValue, end);
}
After 103917, its value doesnot get updated and it gives memory protection failure.
Please let me know where I am doing something wrong as it seems so trivial to fix it.
Thanks,
Neha.
Well, I did not really try to understand the code, but I saw some big numbers and the word recursion.
So I would guess its a stack overflow because your recursion is too deep?

Resources