Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Comonadically building Pascal's Triangle

Name: Anonymous 2012-10-18 12:18

I was reading [1] and one of the comments suggested implementing Pascal's Triangle comonadically as an exercise. However the approach I'm trying to take doesn't work out since I can't find an un-arbitrary implementation for coreturn of T's Even constructor, and I /don't/ want to represent Pascal's Triangle with a 2D grid, as in [1]'s U type.


class Functor w => Comonad w where
    coreturn :: w a -> a
    cojoin   :: w a -> w (w a)
    (=>>)    :: w a -> (w a -> b) -> w b
    x =>> f = fmap f $ cojoin x

data T x = Odd  [x]   x   [x]
         | Even [x] (x,x) [x]

instance Functor T where
    fmap f (Odd  l    m    r) = Odd  (fmap f l)    (f m)     (fmap f r)
    fmap f (Even l (ml,mr) r) = Even (fmap f l) (f ml, f mr) (fmap f r)

instance Comonad T where
    coreturn (Odd  _ m _) = m
    coreturn (Even ? ? ?) = ?


So, what can I do? Is there even a sensible instance of Comonad for the representation I've chosen, or do I just /have/ to use a grid-like structure for the data type?

In joyful anticipation of your illuminating comments, a fellow /prog/rider.

[1] http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html

Name: Anonymous 2012-10-22 0:20

>>20
The equivalent APL:
(⍳6)∘.!⍳6
NARS2000 has an extension allowing ∘.!⍨⍳6
This Pascal's triangle is sideways. Does J have a column-major ordering?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List