0

I would like to implement a function which gets as input f g a when f g are lambda functions and a is a parameter. The function should do as following:

fun foo f g a = if (g a) then (f a) else a;
> val foo = fn : ('a -> 'a) -> ('a -> bool) -> 'a -> 'a

Is it possible to somehow replace if-else with andalso,orelse, or some other method? I would like to implement a function without using if-else statements.

Edit: Looking for a way to combine two functions, one of them is 'a -> 'a while the other one is 'a -> bool.

3
  • This sounds like an X-Y problem. Could you change the question to the problem rather than the attempted solution? Commented Dec 1, 2018 at 9:33
  • @SimonShine Thanks for the reply. It actually straightforward. I implement an anonymous function (fn x => if (g x) then (f x) else x) and I would like somehow to remove the if-else statements. Commented Dec 1, 2018 at 14:32
  • Since a and f a have type 'a, I'm not sure if there is a more elegant way to do it. (You can only replace if-then-else with andalso/orelse when the type of the branches are bool.) But maybe changing the names and order of the identifiers will make it more readable. I'd still like to see the context. Commented Dec 2, 2018 at 9:13

1 Answer 1

2

if for some weird reason you don't want to use if-else you could pattern match to true/false. E.g. like that:

fun bar f g a = case (g a) of
                true => (f a)
                | false => a;

fun bari f g a = (fn true => f a | false => a)(g a);

But as Simon Shine already mentioned you may want to rephrase your question and describe the problem in a bigger picture if this is not what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.