Skip to content

Instantly share code, notes, and snippets.

@jvranish
Created October 10, 2011 13:41
Show Gist options
  • Save jvranish/1275359 to your computer and use it in GitHub Desktop.
Save jvranish/1275359 to your computer and use it in GitHub Desktop.
Example of some nice properties of fixed lists due to them being applicative functors
import Data.FixedList -- this doesn't come with haskell, do a 'cabal install fixed-list' to install it
-- or get it from here: http://hackage.haskell.org/package/fixed-list-0.1.5
-- more documentation on this library is here: http://hackage.haskell.org/packages/archive/fixed-list/0.1.5/doc/html/Data-FixedList.html
-- These are all built in haskell modules
import Control.Applicative
import Data.Foldable
import Prelude hiding (sum, concat)
myList = 1 :. 3 :. 5 :. Nil
-- |[1,3,5]|
myOtherList = 2 :. 1 :. 9 :. Nil
-- |[2,1,9]|
myListPlusOne = myList + 1
-- |[2,4,6]|
listsAdded = myList + myOtherList
-- |[3,4,14]|
listOfLists = myList :. myOtherList :. myListPlusOne :. Nil
-- |[|[1,3,5]|,|[2,1,9]|,|[2,4,6]|]|
listOfListsPlusOne = listOfLists + 1
-- |[|[2,4,6]|,|[3,2,10]|,|[3,5,7]|]|
dotProductTest = sum $ myList * myOtherList
-- 50
concatElemsAsStrings = concat $ show <$> myList
-- "135"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment