[Haskell-cafe] FP design

Jonathan Cast jonathanccast at fastmail.fm
Mon Nov 5 19:49:41 EST 2007


On Tue, 2007-11-06 at 10:32 +1030, Levi Stephen wrote:
> Hi,
> 
> I'm was wondering how most people work during when designing a functional 
> program. Do you create data structures/types first? Do you work from some type 
> signatures?
> 
> For example, take a blog. Is the first step likely to be something like:
> 
> data BlogEntry = BlogEntry { title::String,content::String,comments::[Comment] }
> type Blog = [BlogEntry]
> 
> or more likely thinking about what functions will be required:
> 
> addEntry :: BlogEntry -> Blog -> Blog
> displayBlog :: Blog -> HTML
> displayEntry :: BlogEntry -> HTML
> 
> I'm trying to get my brain out of thinking OO thinking more functionally.

I typically start with a list of the types I want, then the minimal list
of type signatures.  Ideally, it should be possible to write an
arbitrarily large set of programs in the given application domain by
composing this initial list of functions (so the data type can be
specified abstractly, if desired).

Then I define the bodies of the types, then I find out that I can't
define the functions elegantly, so I start over from the top.  Once my
function definitions are natural, I know I'm done, so I can start
writing derived functions (key insight: most functions in FP don't need
access to the representation of your data types.  This is the
counter-part, if you will, of encapsulation in OO: well-encapsulated
types are those where most code can be written by composing a short list
of primitives with direct access to the type.)

jcc




More information about the Haskell-Cafe mailing list