Expressing Abstraction - Abstracting Expression
Recorded at:
Very interesting talk
by
Faisal Waris
To provide a flavor, below is the main recursive loop of an experimental HTML parser written with F# 'Active Patterns' (note WS=whitespace; GT = '>'; LT = '<'; Slash = '/', etc.)
let rec (|Html|) inp acc =
match inp with
| WS (ws, rest) -> (|Html|) rest (WS(ws)::acc)
| LT (Exclaim (PI (p, (GT rest)))) -> (|Html|) rest (PI(p)::acc)
| Script (attrs,body,rest) -> (|Html|) rest (Script(attrs,body)::acc)
| LT (Name (n, Attributes (attrs, IgnoreWS restof))) ->
match restof with
| Slash (GT rest) -> (|Html|) rest (Tag(n,attrs)::acc)
| GT rest -> (|Html|) rest (StartTag(n,attrs)::acc)
| _ -> errorOut restof acc
| LT (Slash (Name (n, (IgnoreWS (GT rest))))) -> (|Html|) rest (EndTag(n)::acc)
| Content (cntnt,rest) -> (|Html|) rest (Content(cntnt)::acc)
| Eof _ -> acc|>List.rev
| _ -> errorOut inp acc
When a pattern is matched, the right side of the "->" is executed. In this case the loop is just collecting html tags in a structured way.
More here fshtml.codeplex.com
Re: Very interesting talk
by
Faisal Waris
I started with the algorithm paper by Michael Porter and just transcribed that to code, section by section. This is good example of expressability in my opinon.
Details here: fwaris.wordpress.com/2012/10/30/porter-stemmer-...




Hello stranger!
You need to Register an InfoQ account or Login to post comments. But there's so much more behind being registered.Get the most out of the InfoQ experience.
Tell us what you think