Compamable functions in okamla

How can I define a compound function in a functional language, in particular using Ocaml? For example, if I write a function that calculates the negation of the result of another function, that is: not(f(x)) where f(x) returns a boolean value. How to determine this?

+8
functional-programming function-composition ocaml
source share
3 answers

For some function f , which is of type:

 f: 'a -> bool 

you want to be able to generate another function to wrap it in order to negate the result. Let's look at the type of this new function, call it negated (I do not use not , since this is the name of the built-in):

 negated: ('a -> bool) -> 'a -> bool 

Why is this type? Why not 'a -> bool ? Remember well, we want this new function to use an existing function and return a new function with the same type that does something else. To make this clearer, you can think like this: ('a -> bool) -> ('a -> bool) , which is equivalent.

So now, given these limitations, how can we write a negated function?

 let negated f = ?? 

Well, first you need to consider that you need to return a function to this function:

 let negated f = (fun x -> ??) 

What's next? Well, we know that the new function we create must call our wrapped function an argument and negate it. Let this be done, call the function with the argument: fx and cancel it: not (fx) . This gives us the final definition of the function:

 let negated f = (fun x -> not (fx)) 

Look at him in action:

 # let fx = x < 5;; val f : int -> bool = <fun> # f 2;; - : bool = true # f 8;; - : bool = false # let negated f = (fun x -> not (fx));; val negated : ('a -> bool) -> 'a -> bool = <fun> # let g = negated(f);; val g : int -> bool = <fun> # g 2;; - : bool = false # g 8;; - : bool = true 
+12
source share

I'm not sure exactly how far you are going to go here - the code you wrote will work fine. Therefore, I will give a simple step by step about how you write this material from scratch. A simple negation is simple:

 let not = function | true -> false | false -> true 

You can write not (fx) and this will give you a negation of the result of fx .

For a function that combines functions, you can use:

 let comp fgx = f (gx) 

So we can do:

 let even n = match n mod 2 with | 0 -> true | _ -> false let odd = comp not even 
+5
source share

Wow, what's with all these overly complicated answers? What happened with:

 let compose fgx = g (fx) 

To get g(x) = not(f(x)) if you have f : 'a -> bool :

 let g = compose not f 

In addition, you can do cool things like:

 let composite_function = let id x = x in let transforms = [ (fun n -> n + 1); (fun n -> n * 2); (fun n -> n * n) ] in List.fold_left compose id transforms 

Now composite_function is of type int -> int , and its efficient definition is:

 let composite_function n = let n2 = (n + 1) * 2 in n2 * n2 

EDIT: Oh, I think Chuck really did it. I probably shouldn't have just filmed. Anyway, it seems to me that I like to add up the layout function, so I will continue this .: P

+3
source share

All Articles