﻿ Caml Weekly News Previous week Up Next week

Hello

Here is the latest Caml Weekly News, for the week of June 24 to July 1, 2008.

### patterns v0.4

Continuing the thread from last week, Nathaniel Gray said:
```> Can someone summarise active patterns for us?  The MSDN site
> containing the paper is down at the moment.

Sure.  Active patterns are a lot like Wadler's "views", and thus are
similar to the stuff in micmatch.  With active patterns you can invoke
functions (implicitly) within pattern matches and match the results,
which allows you to provide a "virtual" algebraic data structure for
any value you might want to match against.  Here's an example from the
paper (using F# syntax):

open LazyList
let (|Cons|Nil|) l = if nonempty(l) then Cons(hd(l),tl(l)) else Nil

let rec pairSum xs =
match xs with
| Cons (x, Cons (y,ys)) -> consl (x+y) (lazy (pairSum ys))
| Cons (x, Nil ()) -> consl x (lazy nil)
| Nil () -> nil

This expands to:
let rec pairSum xs =
if nonempty xs then
let x, ys = hd xs, tl xs
if nonempty ys then
let y, zs = hd ys, tl ys
consl (x+y) (lazy (pairSum zs))
else consl x (lazy nil) )
else nil

There are other variations presented in the paper, including
parameterized patterns.  These are nice for doing things like regular
expression matching.  Again, from the paper:

let (|ParseRE|_|) re s =
let m = Regex(re).Match(s)
if m.Success
then Some [ for x in m.Groups -> x.Value ]
else None

let swap s =
match s with
| ParseRE "(\w+)-(\w+)" [l;r] -> r ^ "-" ^ l   (* See below *)
| _ -> s

The matching syntax here is a bit confusing because it can be hard to
tell where parameters end and patterns begin.  In the example above,
"(\w+)-(\w+)" is a parameter and [l;r] is a pattern.  There's
definitely room for improvement over this syntax.

There are other variations and examples in the paper.  I'd definitely
recommend reading it.  If you still can't get it from the website I
can forward you a copy off-list.
```

### ocamlhackers.ning.com is open

Martin Jambon announced:
```I couldn't resist creating an OCaml social network at Ning:

http://ocamlhackers.ning.com/

It's free and easy. Allows you to have your OCaml blog and exclusively OCaml
```

### OCaml PLEAC reaches 70%

Dave Benjamin announced:
```The PLEAC project aims to translate the source code examples of the Perl
Cookbook to many programming languages. I have been working steadily for the
past two years toward completing the OCaml translation. As of today, it is
70.71% complete, in between Ruby (64.43%) and Python (85.43%).

http://pleac.sourceforge.net/
http://pleac.sourceforge.net/pleac_ocaml/index.html

Much of my recent work has been on the file I/O chapters, which cover the
topics of reading and writing to files using Pervasives and the Unix module.
The file access chapter covers argument parsing, file locking, buffering and
non-blocking I/O:

http://pleac.sourceforge.net/pleac_ocaml/fileaccess.html

The file contents chapter contains some helpful examples of working with
Streams and Buffers, line-indexing of large files, and manipulation of binary
data including an example of using Richard Jones' Bitmatch library to parse
and "tail" Linux's binary "utmp" database of login events:

http://pleac.sourceforge.net/pleac_ocaml/filecontents.html

http://ramenlabs.com/pleac-pdf/pleac_ocaml.pdf

As always, feedback, corrections, and contributions are more than welcome, and
I will do my best to make suggested improvements. I think that, despite being
somewhat Perl-centric and in need of more explanation, the OCaml PLEAC has
already become a valuable resource. I refer to it frequently myself. Hopefully
some day there will be a real OCaml Cookbook. In the meantime, there are a lot
of practical code snippets that can save a few trips to the manual / interface
files. I hope you find it useful as well.
```

### Using folding to read the cwn in vim 6+

Here is a quick trick to help you read this CWN if you are viewing it using vim (version 6 or greater).

`:set foldmethod=expr:set foldexpr=getline(v:lnum)=~'^=\\{78}\$'?'&lt;1':1zM`

If you know of a better way, please let me know.

### Old cwn

If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.

Alan Schmitt