caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Simple idea for making a function infix
@ 2006-11-14  4:29 oleg
  0 siblings, 0 replies; 2+ messages in thread
From: oleg @ 2006-11-14  4:29 UTC (permalink / raw)
  To: caml-list


Haskell's back-quote notation has a notable limitation as it applies
to identifiers only. That is, we gain infix identifiers rather than
infix expressions. As it turns out, we can obtain infix expressions
without any change of syntax or any backticks:

  http://www.haskell.org/pipermail/haskell-prime/2006-March/000935.html
  http://www.haskell.org/haskellwiki/Infix_expressions

It seems that the second solution of the above page is similar to the
one just proposed for OCaml. For ease of comparison, in Haskell, ($) is
defined as [x $ y = x y] and flip flips the order of the arguments.

The first solution on that page can be rendered in Ocaml as follows:

	let (>--) x y = (x,y);;
	let (<--) (x,f) y = f x y;;

Now we can write

	# 3 >-- min <-- 4;;
	- : int = 3

We gain not only infix identifiers but infix expressions as well:

	let a = Array.make 3 'a';;
	val a : char array = [|'a'; 'a'; 'a'|]
	1 >-- Array.set a <-- 'b'; a;;
	- : char array = [|'a'; 'b'; 'a'|]

so we can use even three-argument functions as sort of `infix'...



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Simple idea for making a function infix
@ 2006-11-13  7:23 Keisuke Nakano
  0 siblings, 0 replies; 2+ messages in thread
From: Keisuke Nakano @ 2006-11-13  7:23 UTC (permalink / raw)
  To: caml-list

Hi all,

Haskell people sometimes complain about that OCaml cannot make an
arbitrary function infix. For example, they can write (3 `min` 4)
to get the result of (min 3 4) in Haskell. Can we satisfy them
without changing OCaml's syntax?

Here is a simple idea for making a function infix in OCaml.
I hope it will be useful for those who like Haskell's backquote
notation `function_name`.
The idea doesn't require any change of OCaml's syntax.

We use the following two infix operators.

   let ( /* ) x y = y x
   and ( */ ) x y = x y

Then we can make an infix operator /*f*/ for a binary function f.
For example, using binary functions 'min' and 'max', we can write

   3 /*min*/ 4 + 6 /*max*/ 8

to get 11 as 'min 3 4 + max 5 8'. Note that the infix operator
( */ ) may conflict with Num.( */ ) if the Num module is loaded
and opened. You can use other definitions in a similar manner, though.

You have to take care of the precedence. For example,

   3 /*min*/ 4 * 6 /*max*/ 8

will return 18 as 'max ((min 3 4) * 6) 8'. So we should write

   (3 /*min*/ 4) * (6 /*max*/ 8)

to get 24 as 'min 3 4 * max 6 8'.


The original idea was introduced in my blog a few months ago
(written in Japanese, though). At that time, I used other definitions:

   let ( <| ) x y = y x
   and ( |> ) x y = x y

or

   let ( @^ ) x y = y x
   and ( ^@ ) x y z = x z y

where the definition of ( ^@ ) should be given in a different way
because of the precedence of infix operaters starting with '^' or '@'.
These operators perform a different behavior because of the precedences
of operators.

   3 <|min|> 4 + 6 <|max|> 8 (* = max (min 3 (4 + 6)) 8 => 8 *)
   3 @^min^@ 4 + 6 @^max^@ 8 (* = min 3 (max (4 + 6) 8) => 3 *)

So you have to write

   (3 <|min|> 4) + (6 <|max|> 8)
   (3 @^min^@ 4) + (6 @^max^@ 8)

to get 11 as min 3 4 + max 5 8'.


Sincerely,

-----------------------------------------------------------------------
Keisuke Nakano
Department of Mathematical Informatics,
University of Tokyo


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-11-14  4:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-14  4:29 Simple idea for making a function infix oleg
  -- strict thread matches above, loose matches on Subject: below --
2006-11-13  7:23 Keisuke Nakano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).