Monthly Archives: December 2012

Haskell : Monads for States!

Demonstrate how to carry to implicit states with Monads : import System.Random import Control.Monad.State type RandomState = State StdGen getRandom ::(Random a) => RandomState a getRandom = get >>= \g -> let (a,g’) = random g in put g’ >> … Continue reading

Posted in Uncategorized | Leave a comment

Haskell : permutation fxn using Monadic lifters

import Control.Monad combo :: [[a]]->[[a]]->[[a]] combo = liftM2 (++) combiner :: [[a]]->[[a]] combiner xs = (foldr (.) id $ take (n-1) $ cycle [combo xs] ) xs where n = length xs this does permutations samples — combiner [“a”,”b”] [“aa”,”ab”,”ba”,”bb”] … Continue reading

Posted in Uncategorized | Leave a comment

Haskell : Zippers with zippers

Haskell ZipWithN in termas of zipWithN-1: zip’ :: [a]->[b]->[(a,b)] zip’ xs ys = zipWith (\x y -> (x,y) ) xs ys zipWith3′ :: (a->b->c->d)->[a]->[b]->[c]->[d] zipWith3′ f xs = zipWith id . zipWith f xs zipWith4′ :: (a->b->c->d->e)->[a]->[b]->[c]->[d]->[e] zipWith4′ f xs … Continue reading

Posted in Uncategorized | Leave a comment

Haskell : Monad Lifters

liftM’ :: Monad m => (a->r)->m a -> m r liftM’ f m = m >>= \a-> return (f a) liftM2′ :: Monad m => (a->b->r)->m a -> m b -> m r liftM2′ f m1 m2 = liftM’ f … Continue reading

Posted in Uncategorized | Leave a comment

Haskell : more Lifting !

one of useful lifters for Monads are : LiftM ..family liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r But not very good for chaining in ,a better derivative for … Continue reading

Posted in Uncategorized | Leave a comment

Haskell :folding an Array

import Data.Array lazy folding foldlA f zero a = go f zero (indices a) where go f zero (x:xs) = go f (f zero (a ! x)) xs go _ zero [] = zero in-place evaluation using seq foldlA’ f … Continue reading

Posted in Uncategorized | Leave a comment