Haskell foldl as foldr

foldl :: (a -> b -> a) -> a -> [b] -> a
foldl f init xs = foldr f’ id xs init
where f’ x g z = g ( f z x )

at first definition of f’ looks puzzling , but if we looks at step fxn in foldr :
a->b->b

so (in easy to say words ) f’ is just preserving order of operands and with foldr we are creating a chain .

Advertisements
Posted in Uncategorized | 1 Comment

Haskell map fxn as foldr

Standard haskell map fxn def :
map’ mapper [] = []
map’ mapper (x:xs) = mapper x : map’ mapper xs

same map as in terms of foldr :
map” mapper = foldr (\x y -> mapper x : y ) []

Posted in Uncategorized | Leave a comment

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’ >> return a

twoRands ::(Random a,Random b)=> RandomState (a,b)
twoRands = liftM2 (,) getRandom getRandom

getRandoms :: (Random a) =>RandomState [a]
getRandoms = getRandom >>= \a -> getRandoms >>= (\xs -> return (a:xs))

fullRands rand= getStdRandom (runState rand)

getRandom’ ::(Random a) => RandomState a
getRandom’ = do
g RandomState (a,b)
twoRands’ = do
a<-getRandom'
b RandomState (a,b)
twoRands” = getRandom >>= \a->
getRandom >>= \b -> return (a,b)

threeRands :: (Random a,Random b,Random c)=> RandomState (a,b,c)
threeRands = do
(a,b) <- twoRands
c<- getRandom
return (a,b,c)

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”]

combiner [“a”,”b”,”c”]
[“aaa”,”aab”,”aac”,”aba”,”abb”,”abc”,”aca”,”acb”,
“acc”,”baa”,”bab”,”bac”,”bba”,”bbb”,”bbc”,”bca”,”bcb”,”bcc”,”caa”,”cab”,”cac”,
“cba”,”cbb”,”cbc”,”cca”,”ccb”,”ccc”]

combiner [“a”]
[“a”]

combiner []
[]

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 = zipWith3′ id . zipWith f xs

zipWith5′ :: (a->b->c->d->e->f)->[a]->[b]->[c]->[d]->[e]->[f]
zipWith5′ f xs = zipWith4′ id . zipWith f xs

zipWith6′ :: (a->b->c->d->e->f->g)->[a]->[b]->[c]->[d]->[e]->[f]->[g]
zipWith6′ f xs = zipWith5′ id . zipWith f xs

zipWith7′ :: (a->b->c->d->e->f->g->h)->[a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]
zipWith7′ f xs = zipWith6′ id . zipWith f xs

zipWith8′ :: (a->b->c->d->e->f->g->h->i)->[a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]->[i]
zipWith8′ f xs = zipWith7′ id . zipWith f xs

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 m1 >>= \f1 ->
m2 >>= \a1 -> return (f1 a1)

liftM3′ :: Monad m => (a->b->c->r) -> m a -> m b -> m c -> m r
liftM3′ f m1 m2 m3 = liftM2′ f m1 m2 >>= \f1 ->
m3 >>= \a1 -> return (f1 a1)

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 chaining – is ap from Control.Adaptive.
This implementation is :

myap :: (Monad m ) => m (a->b) -> m a -> m b
myap = liftM2 id

myap’:: (Monad m) => m (a->b) -> m a -> m b
myap’ = liftM2 ($)

Posted in Uncategorized | Leave a comment