[Haskell-cafe] In-place modification

Jon Harrop jon at ffconsultancy.com
Sun Jul 15 20:12:18 EDT 2007


On Sunday 15 July 2007 21:26:49 Sebastian Sylvan wrote:
> Can you find me a list of the contests?

No. I've been staring at that site for about 15 minutes and could only find a 
single challenge that was a trivial function from graph theory.

You might like this symbolic simplifier benchmark:

  http://www.lambdassociates.org/studies/study10.htm

The OCaml program is simply:

let rec ( +: ) f g = match f, g with 
   | `Int n, `Int m -> `Int (n +/ m) 
   | `Int (Int 0), e | e, `Int (Int 0) -> e 
   | f, `Add(g, h) -> f +: g +: h 
   | f, g -> `Add(f, g) 

let rec ( *: ) f g = match f, g with 
   | `Int n, `Int m -> `Int (n */ m) 
   | `Int (Int 0), e | e, `Int (Int 0) -> `Int (Int 0) 
   | `Int (Int 1), e | e, `Int (Int 1) -> e 
   | f, `Mul(g, h) -> f *: g *: h 
   | f, g -> `Mul(f, g) 

let rec simplify = function 
   | `Int _ | `Var _ as f -> f 
   | `Add (f, g) -> simplify f +: simplify g 
   | `Mul (f, g) -> simplify f *: simplify g

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e


More information about the Haskell-Cafe mailing list