It's not really comparable, but minimalistic spreadsheet applications always remind me of Dan Piponi's Haskell `loeb` function [0]---a "one-line" "spreadsheet" "implementation". The blog post is very interesting to read, but here's the meat. We're looking for a function with type loeb :: Functor f => f (f a -> a) -> f a and without thinking about the meaning of it, we can implement it as loeb x = fmap (\a -> a (loeb…
One more cool trick from the same article (slightly modified). It's a factorial function computed on an "infinite spreadsheet". But how? fact = loeb fact' where fact' 0 _ = 1 fact' n f = n*f (n-1) If we ask GHC for the type of `fact'` then we see that it is Int -> ((Int -> Int) -> Int) which we can interpret via the `Reader Int` monad as being m (m Int -> Int) -- for m a = (Int -> a) Now, `f :: (Int -> a)` as a funct…
data Stream a = Stream a (Stream a)
deriving Functor
tabulate :: (Int -> a) -> Stream a
tabulate f = go 0 where
go n = Stream (f n) (go (succ n))
index :: Stream a -> (Int -> a)
index (Stream a _) 0 = a
index (Stream a st) n = index st (pred n)
-- btw: tabulate . index == id
-- and index . tabulate == id
fact :: Int -> Int
fact n = index (loeb facts) n where
facts :: Stream (Stream Int -> Int)
facts = tabulate $ \i stream -> i * index stream (i-1)