caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Function definition with multiple patterns in multiple equations
@ 2002-01-08 13:08 ` José Romildo Malaquias
  2002-01-08 13:16   ` Francois Thomasset
                     ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: José Romildo Malaquias @ 2002-01-08 13:08 UTC (permalink / raw)
  To: caml-list

Hello.

I am trying to define a function in OCaml with multiple equations (rules)
and multiple patterns in each equation. I have tried the 3 forms below,
without success.

	let f 0 0 = 1
	  | f _ _ = 0

	let f = fun 0 0 -> 1
          | _ _ -> 0

	let f = function 0 0 -> 1
	  | _ _ -> 0

None of them seems to be supported by the language. In SML, one can write

	fun f 0 0 = 1
	  | f _ _ = 0

What is the equivalent in OCaml?

Romildo
-- 
Prof. José Romildo Malaquias               Departamento de Computação
http://iceb.ufop.br/~romildo       Universidade Federal de Ouro Preto
romildo@iceb.ufop.br                                           Brasil
romildo@uber.com.br
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Function definition with multiple patterns in  multiple equations
  2002-01-08 13:08 ` [Caml-list] Function definition with multiple patterns in multiple equations José Romildo Malaquias
@ 2002-01-08 13:16   ` Francois Thomasset
  2002-01-08 13:18   ` Clement Renard
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Francois Thomasset @ 2002-01-08 13:16 UTC (permalink / raw)
  To: caml-list

> I am trying to define a function in OCaml with multiple equations (rules)
> and multiple patterns in each equation.
It looks like this is what you want:
let f x = function
  0 when x = 0 -> 1
  | _ -> 0;;

-- 
					François Thomasset.
					INRIA (A3)

Tel: +33 (1) 39-63-54-75
Fax: +33 (1) 39-63-53-30 ou +33 (1) 39-63-59-95
Email: Francois.Thomasset@inria.fr
Smail: INRIA, Rocquencourt, BP 105, 78153 Le Chesnay Cedex, France


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Function definition with multiple patterns in multiple equations
  2002-01-08 13:08 ` [Caml-list] Function definition with multiple patterns in multiple equations José Romildo Malaquias
  2002-01-08 13:16   ` Francois Thomasset
@ 2002-01-08 13:18   ` Clement Renard
  2002-01-08 13:26   ` John Prevost
  2002-01-08 13:30   ` Remi VANICAT
  3 siblings, 0 replies; 8+ messages in thread
From: Clement Renard @ 2002-01-08 13:18 UTC (permalink / raw)
  To: José Romildo Malaquias; +Cc: caml-list

On Tue, 8 Jan 2002, José Romildo Malaquias wrote:

> Hello.
> 
> I am trying to define a function in OCaml with multiple equations (rules)
> and multiple patterns in each equation. I have tried the 3 forms below,
> without success.
> 
> 	let f 0 0 = 1
> 	  | f _ _ = 0
> 
> 	let f = fun 0 0 -> 1
>           | _ _ -> 0
> 
> 	let f = function 0 0 -> 1
> 	  | _ _ -> 0
> 
> None of them seems to be supported by the language. In SML, one can write
> 
> 	fun f 0 0 = 1
> 	  | f _ _ = 0
> 
> What is the equivalent in OCaml?

For example :

let f x y = match x,y with
 0,0 -> 1
|_,_ -> 0 


Clement Renard

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Function definition with multiple patterns in multiple equations
  2002-01-08 13:08 ` [Caml-list] Function definition with multiple patterns in multiple equations José Romildo Malaquias
  2002-01-08 13:16   ` Francois Thomasset
  2002-01-08 13:18   ` Clement Renard
@ 2002-01-08 13:26   ` John Prevost
  2002-01-11  9:23     ` Xavier Leroy
  2002-01-11  9:47     ` Andreas Rossberg
  2002-01-08 13:30   ` Remi VANICAT
  3 siblings, 2 replies; 8+ messages in thread
From: John Prevost @ 2002-01-08 13:26 UTC (permalink / raw)
  To: caml-list; +Cc: romildo

On Tue, Jan 08, 2002 at 11:08:36AM -0200, José Romildo Malaquias wrote:

>None of them seems to be supported by the language. In SML, one can write
>
>	fun f 0 0 = 1
>	  | f _ _ = 0
>
>What is the equivalent in OCaml?

There is no perfect equivalent in O'Caml.  Here, there are two ways
to write function definitions.  One of them allows multiple arguments
to be specified:

let f = fun p1 p2 p3 ... -> e

(which is equivalent to)

let f p1 p2 p3 ... = e

The other allows multiple patterns to be specified for one argument:

let f = function
  | p1 -> e1
  | p2 -> e2
  | p3 -> e3
  | ...

(which is equivalent to)

let f x = match x with
  | p1 -> e1
  | p2 -> e2
  | p3 -> e3
  | ...

At first, I was rather upset with this, but I've come to appreciate
it, since it can in fact lead to rather better code for some complex
cases by making you think about what you're actually trying to do.
Then again, for the very simple cases like yours, it does indeed
get in the way a bit.

I still feel it may be a better choice, since in SML you can write:

fun foo a b = e1 a b
  | foo c   = e1 c

(for example.)  This, IMO, can lead to some really strange confusion.

Anyway, the quick-n-dirty method of doing multiple patterns like
your example is to write:

let f a b = match (a,b) with
  | (0, 0) -> 1
  | (_, _) -> 0  

But, this may very well have the overhead of actually building a
pair.  You can also decompose it into something like:

let f = function
  | 0 -> (function
            | 0 -> 1
            | _ -> 0)
  | _ -> 0

Which is, yes, rather unattractive for such a small example.

Hope this helps,

John.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Function definition with multiple patterns in multiple equations
  2002-01-08 13:08 ` [Caml-list] Function definition with multiple patterns in multiple equations José Romildo Malaquias
                     ` (2 preceding siblings ...)
  2002-01-08 13:26   ` John Prevost
@ 2002-01-08 13:30   ` Remi VANICAT
  2002-01-09 13:28     ` [Caml-list] teil recursive function and GC Christophe Raffalli
  3 siblings, 1 reply; 8+ messages in thread
From: Remi VANICAT @ 2002-01-08 13:30 UTC (permalink / raw)
  To: caml-list

José Romildo Malaquias <romildo@uber.com.br> writes:

> Hello.
> 
> I am trying to define a function in OCaml with multiple equations (rules)
> and multiple patterns in each equation. I have tried the 3 forms below,
> without success.
> 
> 	let f 0 0 = 1
> 	  | f _ _ = 0
> 
> 
> What is the equivalent in OCaml?

let f x y = 
   match (x, y) with
   | 0, 0 -> 1
   | _ -> 0

by the way, the compiler make the obvious optimization (it doesn't
build the tuple)

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* [Caml-list] teil recursive function and GC
  2002-01-08 13:30   ` Remi VANICAT
@ 2002-01-09 13:28     ` Christophe Raffalli
  0 siblings, 0 replies; 8+ messages in thread
From: Christophe Raffalli @ 2002-01-09 13:28 UTC (permalink / raw)
  Cc: caml-list


Hello,

It seems that the two follwing functions are not equivalent: the tail
recursive version keeps pointers on all the intermediate sets and use a
huge amount of memory (with around 200000 triangles) while the
imperative version works well.

I think the compiler should generate code that do not keep useless
pointer accessible by the GC for tail recursive function calls (and also
for other function call) ?

(I use ocaml-3.03 with the native code compiler)

Imperative version (it is only a piece of my code):

  let vertices_number = 
    let adone = ref Vertex_set.empty in
    let count_vertices (p1,p2,p3) =
      adone := Vertex_set.add p1 
		       (Vertex_set.add p2
			  (Vertex_set.add p3 !adone))
    in
    List.iter count_vertices triangles;
    Vertex_set.cardinal !adone
  in
  let edges_number = 
    let adone = ref Edge_set.empty in
    let count_edges (p1,p2,p3) =
      adone := Edge_set.add (p1,p2)
		         (Edge_set.add (p2,p3)
			  (Edge_set.add (p1,p3) !adone))
    in
    List.iter count_edges triangles;
    Edge_set.cardinal !adone
  in

Tail recursive version:

  let rec count_vertices adone ts =
    match ts with 
      [] -> Vertex_set.cardinal adone
    | (p1,p2,p3)::ts -> count_vertices (Vertex_set.add p1 
		       (Vertex_set.add p2
			  (Vertex_set.add p3 adone))) ts
  in
  let vertices_number = count_vertices Vertex_set.empty triangles in
  let rec count_edges adone ts =
    match ts with 
      [] -> Edge_set.cardinal adone
    | (p1,p2,p3)::ts -> count_edges (Edge_set.add (p1,p2)
		         (Edge_set.add (p2,p3)
			  (Edge_set.add (p1,p3) adone ))) ts
  in
  let edges_number = count_edges Edge_set.empty triangles in


-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Function definition with multiple patterns in multiple equations
  2002-01-08 13:26   ` John Prevost
@ 2002-01-11  9:23     ` Xavier Leroy
  2002-01-11  9:47     ` Andreas Rossberg
  1 sibling, 0 replies; 8+ messages in thread
From: Xavier Leroy @ 2002-01-11  9:23 UTC (permalink / raw)
  To: John Prevost; +Cc: caml-list, romildo

> Anyway, the quick-n-dirty method of doing multiple patterns like
> your example is to write:
> 
> let f a b = match (a,b) with
>   | (0, 0) -> 1
>   | (_, _) -> 0  
> 
> But, this may very well have the overhead of actually building a
> pair.

Not in Objective Caml.  There's a special optimization in both the
bytecode and native-code compilers to avoid the construction of the
pair in this case.

  You can also decompose it into something like:
> 
> let f = function
>   | 0 -> (function
>             | 0 -> 1
>             | _ -> 0)
>   | _ -> 0
> 
> Which is, yes, rather unattractive for such a small example.

That should be:
 let f = function
   | 0 -> (function
             | 0 -> 1
             | _ -> 0)
   | _ -> (function _ -> 0)

And it would be less efficient due to the extra function invocation
involved.  (Assuming f is generally applied to two arguments; if you
do a lot of partial applications of f, the latter might be slightly
more efficient.)

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Function definition with multiple patterns in multiple  equations
  2002-01-08 13:26   ` John Prevost
  2002-01-11  9:23     ` Xavier Leroy
@ 2002-01-11  9:47     ` Andreas Rossberg
  1 sibling, 0 replies; 8+ messages in thread
From: Andreas Rossberg @ 2002-01-11  9:47 UTC (permalink / raw)
  To: caml-list

John Prevost wrote:
> 
> I still feel it may be a better choice, since in SML you can write:
> 
> fun foo a b = e1 a b
>   | foo c   = e1 c

No, you can't. All function rules must have the same number of argument
patterns.

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

"Computer games don't affect kids; I mean if Pac Man affected us
 as kids, we would all be running around in darkened rooms, munching
 magic pills, and listening to repetitive electronic music."
 - Kristian Wilson, Nintendo Inc.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2002-01-11  9:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <romildo@uber.com.br>
2002-01-08 13:08 ` [Caml-list] Function definition with multiple patterns in multiple equations José Romildo Malaquias
2002-01-08 13:16   ` Francois Thomasset
2002-01-08 13:18   ` Clement Renard
2002-01-08 13:26   ` John Prevost
2002-01-11  9:23     ` Xavier Leroy
2002-01-11  9:47     ` Andreas Rossberg
2002-01-08 13:30   ` Remi VANICAT
2002-01-09 13:28     ` [Caml-list] teil recursive function and GC Christophe Raffalli

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