[personal profile] neelk
I think I've written the most polymorphic non-toy two-line ML program ever.
val cross : (('a -> 'b -> 'c) -> 'd -> 'e -> 'f) ->
            (('g -> 'h -> 'i) -> 'j -> 'b -> 'c) ->
            ('a -> 'g -> 'h -> 'i) -> 
            'e -> 'd -> 'j -> 'f

Here's the definition:

let cross outer inner f init xs ys =
  outer (fun x acc -> inner (fun y acc -> f x y acc) ys acc) xs init



It encapsulates a nested iteration over two arbitrary collections. The first two arguments are folds over two different datatypes, the third and forth arguments are the step function and initial argument, and the last two are the two collections. I am tempted to try and generalize this so it's a combinator that lets you iterate over an arbitrary number of collections, but that seems over the top -- I've already got 10 polymorphic arguments!

Date: 2006-10-20 03:26 am (UTC)
From: [identity profile] jeffwik.livejournal.com
I choose to believe this is some sort of profanity, or obscenity, or both.

Date: 2006-10-20 05:24 pm (UTC)
From: [identity profile] little--one.livejournal.com
You have now succeeded in reminding me why programming in ML makes my head hurt... and why I did so awful in my programming languages class the first time around. (The second time, thankfully, there was a lot less programming in ML and a lot more talk of language theory, which hurts my head much, much less.)

Profile

neelk

March 2007

S M T W T F S
    123
4567 8910
11121314151617
18192021222324
252627 28293031

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 22nd, 2025 02:17 pm
Powered by Dreamwidth Studios