Alex Rivera | Logout

Examples of functional programs 'writing themselves' via type analysis

Asked 2009-11-12T06:34:26.133
10

(Background: I've been thinking about doing a presentation on F# and functional programming. From experience, I think that the 'wow' factor of pattern matching and type inference is not necessarily enough to counteract the 'help!' factor of "where are my curly brackets and semicolons, my code is going to fall off the edge!". Which got me thinking about the real wow factor - for me - which is 1) that if it compiles, generally that means that it works and 2) that you can often infer the implementation from the types)

There is a video on Channel9 with Brian Beckman and Erik Meijer where they mentioned how implementation sometimes just 'falls out' of the type signature of a function. I've also experienced this in the past, but can't come up with a good example that would be sufficiently simple to present to someone with no previous functional experience.

Has anyone got a good example to share? (it doesn't have to be in F#)

UPDATE

If it's any help, I think we need to think about this differently: The actual puzzle is as follows:

I have some data with a given type, I want to transform it to a different type, and I have a set of functions with given signaures.

This is the 'lego' that you have to plug together.

Edit
Report

1 Answer

5

Pipe

Here's a third example...

Suppose I want to write a function

p : 'a -> ('a -> 'b) -> 'b

That is, I take as arguments a value of type 'a, and a function that takes an 'a and returns a 'b. And my result should be a 'b. Well, again, modulo infinite loops and exceptions and default-initialization, there's only one implementation:

let p x f = f x

'p' may not look too useful until you realize it's the pipeline operator (|>).

Hm, I feel like these examples so far are underwhelming.

answered 2009-11-12T07:16:47.920

Your Answer