If-then-else simplified in Ocaml - if-statement

I'm starting in Ocaml and one of the questions that i have is: How to simplify this segment of code
if f (x) < h then f (x) else h

You can write this
min (f x) h
(I hope this isn't a school assignment. Better to figure those out yourself.)

Related

Haskell code to compute sum of squares of negative integers using foldr function

I am new to haskell code. I tried to compute the sum of squares of negative integer in a list using foldr high order.
sumsq :: Int -> Int
sumsq n = foldr op 0 [1..n]
where op x y = x*x + y
Please help to explain each line of code and give any solution if error in this code
When using "where", important to follow the indentation rule.
Here lambda will be appropriate
sumsq n = foldr (\x y -> x*x + y) 0 [1..n]

Standard ML: How to compute x to the power of i?

I am new to Standard ML. I am trying to compute x squared i, where x is a real and i is an non-negative integer. The function should take two parameters, x and i
Here is what I have so far:
fun square x i = if (i<0) then 1 else x*i;
The error that I am getting is that the case object and rules do not agree
The unary negation operator in SML is not - as it is in most languages, but instead ~. That is likely what is causing the specific error you cite.
That said, there are some other issues with this code. L is not bound in the example you post for instance.
I think you may want your function to look more like
fun square (x : real) 0 = 1
| square x i = x * (square x (i - 1))
You'll want to recurse in order to compute the square.

N times function composition

I need to write a code that composes a function, f (x), with itself N times using recursive function.
What I wrote is:
let f x = x + 1 (*it can be any function*)
let rec compose n f x = if n = 0 then
"Can't compose anymore"
else compose (n-1) f (f x);;
which is obviously not right. I know the code is not finished, but I do not know how to continue. Am I on the right path or not? Can you tell me how to solve the problem?
You are on the right path. Based on the requirements, I would try to start from those equations:
compunere 1 f x == f x
The above says that applying f once to x is exactly the same as doing (f x).
compunere 2 f x == f (f x)
Likewise, applying f twice should compute f (f x). If you replace (f x) by a call to compunere, you have:
compunere 2 f x == f (f x) = f (compunere 1 f x)
The general pattern of recursion seems to be:
compunere n f x == f (compunere (n - 1) f x)
Note that the most general type of f is a -> b, but when f is called again with a value of type b, that means that a and b should be the same type, and so f really is an endomorphism, a function of type a -> a. That is the case for N >= 1, but in degenerate case of N=0, you could have a different behaviour.
Applying f zero time to x could mean "return x", which means that compunere could theoretically return a value of type a for zero, for any f being a a -> b function, a and b possibly distinct; you could distinguish both cases with more code, but here we can simply let the typechecker enforce the constraint that a = b in all cases and have an uniform behaviour. You can also make 0 invalid (like negative numbers) by throwing an exception (negative applications could theoretically be postitive applications of the inverse function, but you cannot compute that when knowing nothing about f; f could be non-invertible).
Your code is a little bit different:
compunere 3 f x == (compunere 2 f (f x))
== (compunere 1 f (f (f x)))
== (compunere 0 f (f (f (f x))))
...
The advantage of your approach is that the recursive call to compunere is directly giving the result for the current computation: it is in tail position which allows the compiler to perform tail-call elimination.
When you reach N=0, the value locally bound x gives the result you want. Here, for N=0 as an input, the only natural interpretation is also to return x.

Get result of tactics application as an expression in Z3

Is there anything similar in C++ like Z3py interface's as_expr(). I'm trying to get the result of applying the tactics as a z3 expression, exp, not as type apply_result.
For example, in the below code
context c;
expr x = c.bool_const("x");
expr y = c.bool_const("y");
expr f = ( (x || y) && (x && y) );
solver s(c);
goal g(c);
g.add( f );
tactic t1(c, "simplify");
apply_result r = t1(g);
std::cout << r << "\n";
Also, is there any way to convert the apply_result into z3 expr?
In general, the result of a tactic application is a set of goals. Most tactics produce only one goal, but some produce more than one. For each of those subgoals, you can use as_expr() and then logical-or them together. We can add an as_expr(...) to class apply_result if that helps. (I'm busy with other stuff at the moment; if you add it yourself, submit a pull request, contributions very welcome!)

Making recursive function in OCaml

I want to make a recursive function that sums the integers between two values. I'm doing:
let rec sum_between x y =
if x>y then sum_between y x else
if x=y then x else x + sum_between x+1 y ;;
But I get the error: This expression has type int -> int
but an expression was expected of type int
What am I doing wrong?
Function application has high precedence in OCaml. You need to parenthesize an expression when it's an argument to a function.
Your code
sum_between x+1 y
is parsed like this:
(sum_between x) + (1 y)
You need parentheses:
sum_between (x + 1) y
(Same answer as Edgar Aroutiounian but more helpful detail I hope.)