Skip to content

Instantly share code, notes, and snippets.

@LeoAdamek
Created July 5, 2014 11:47
Show Gist options
  • Save LeoAdamek/b00ecd9380007ee2a666 to your computer and use it in GitHub Desktop.
Save LeoAdamek/b00ecd9380007ee2a666 to your computer and use it in GitHub Desktop.

Revisions

  1. LeoAdamek created this gist Jul 5, 2014.
    10 changes: 10 additions & 0 deletions lazy_matrix.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    acks :: [[Int]]
    acks = [ [ case (m, n) of
    (0, _) -> n + 1
    (_, 0) -> acks !! (m - 1) !! 1
    (_, _) -> acks !! (m - 1) !! (acks !! m !! (n - 1))
    | n <- [0..] ]
    | m <- [0..] ]

    main :: IO ()
    main = print $ acks !! 4 !! 1
    11 changes: 11 additions & 0 deletions performant.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    module Main where

    data P = P !Int !Int

    main :: IO ()
    main = print $ ack (P 4 1) id
    where
    ack :: P -> (Int -> Int) -> Int
    ack (P 0 n) k = k (n + 1)
    ack (P m 0) k = ack (P (m-1) 1) k
    ack (P m n) k = ack (P m (n-1)) (\a -> ack (P (m-1) a) k)
    5 changes: 5 additions & 0 deletions simple_ack.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    main = print $ ack 4 1
    where ack :: Int -> Int -> Int
    ack 0 n = n+1
    ack m 0 = ack (m-1) 1
    ack m n = ack (m-1) (ack m (n-1))