开发者

OCaml: Using a comparison operator passed into a function

I'm an OCaml noob. I'm trying to figure out how to handle a comparison operator that's passed into a function.

My function just tries to pass in a comparison operator (=, <, >, etc.) and an int.

let myFunction comparison x = 
if (x (comparison) 10) then 
   10
else
   x;;

I was hoping that this code would evaluate to (if a "=" were passed in):

if (x = 10) then
   10
else
   x;;

However, this is not working. In particular, it thinks that x is a bool, as evidenced by this error message:

This expression has type 'a -> int -> bool
but an expressi开发者_如何转开发on was expected of type int

How can I do what I'm trying to do?

On a side question, how could I have figured this out on my own so I don't have to rely on outside help from a forum? What good resources are available?


Comparison operators like < and = are secretly two-parameter (binary) functions. To pass them as a parameter, you use the (<) notation. To use that parameter inside your function, you just treat it as function name:

let myFunction comp x = 
  if comp x 10 then 
     10
  else
     x;;

printf "%d" (myFunction (<) 5);; (* prints 10 *)


OCaml allows you to treat infix operators as identifiers by enclosing them in parentheses. This works not only for existing operators but for new ones that you want to define. They can appear as function names or even as parameters. They have to consist of symbol characters, and are given the precedence associated with their first character. So if you really wanted to, you could use infix notation for the comparison parameter of myFunction:

        Objective Caml version 3.12.0
# let myFunction (@) x =
      x @ 10;;
val myFunction : ('a -> int -> 'b) -> 'a -> 'b = <fun>
# myFunction (<) 5;;
- : bool = true
# myFunction (<) 11;;
- : bool = false
# myFunction (=) 10;;
- : bool = true
# myFunction (+) 14;;
- : int = 24
# 

(It's not clear this makes myFunction any easier to read. I think definition of new infix operators should be done sparingly.)

To answer your side question, lots of OCaml resources are listed on this other StackOverflow page:

https://stackoverflow.com/questions/2073436/ocaml-resources


Several possibilities:

Use a new definition to redefine your comparison operator:

let myFunction comparison x =
  let (@) x y = comparison x y in
  if (x @ 10) then 
    10
  else
    x;;

You could also pass the @ directly without the extra definition.

As another solution you can use some helper functions to define what you need:

let (/*) x f = f x
let (*/) f x = f x

let myFunction comparison x =
  if x /* comparison */ 10 then
    10
  else 
    x 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜