English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: Simple idea for making a function infix
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-11-14 (04:30)
From: oleg@p...
Subject: Re: Simple idea for making a function infix

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:


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'...