Skip to main content

Home/ Haskell/ Group items tagged howto

Rss Feed Group items tagged

mesbah095

Guest Post Online - 0 views

  •  
    Article Writing & Guestpost You Can Join this Site for Your Article & guest post, Just Easy way to join this site & total free Article site. This site article post to totally free Way. Guest Post & Article Post live to Life time only for Current & this time new User. http://guestpostonline.com
Javier Neira

A Neighborhood of Infinity: Haskell Monoids and their Uses - 0 views

  • The Writer MonadYou can think of monoids as being accumulators. Given a running total, n, we can add in a new value a to get a new running total n' = n `mappend` a. Accumulating totals is a very common design pattern in real code so it's useful to abstract this idea. This is exactly what the Writer monad allows. We can write monadic code that accumulates values as a "side effect". The function to perform the accumulation is (somewhat confusingly) called tell. Here's an example where we're logging a trace of what we're doing.
  • This is an implementation of the factorial function that tells us what it did.
  • We use runWriter to extract the results back out. If we run> ex1 = runWriter (fact1 10)we get back both 10! and a list of what it took to compute this.
  • ...6 more annotations...
  • and the monoid for addition is Sum
  • but there is a big advantage to using the Writer version. It has type signature f :: Integer -> Writer (Sum Integer) Integer. We can immediately read from this that our function has a side effect that involves accumulating a number in a purely additive way.
  • This is the Bool type with the disjunction operator, better known as ||.
  • "tell my caller if any value of r is ever 120"
  • One last application to mention is the Data.Foldable library. This provides a generic approach to walking through a datastructure, accumulating values as we go. The foldMap function applies a function to each element of our structure and then accumulates the return values of each of these applications. An implementation of foldMap for a tree structure might be
  • Suppose we want to accumulate two side effects at the same time. For example, maybe we want to both count instructions and leave a readable trace of our computation. We could use monad transformers to combine two writer monads. But there is a slightly easier way - we can combine two monoids into one 'product' monoid. It's defined like this:instance (Monoid a,Monoid b) => Monoid (a,b) where mempty = (mempty,mempty) mappend (u,v) (w,x) = (u `mappend` w,v `mappend` x)
Javier Neira

Bluish Coder: Dynamic Compilation and Loading of Modules in Haskell - 0 views

  •  
    The Haskell system GHC has libraries that provide the ability to compile Haskell code and dynamically load it into a running Haskell program. A library that provides this functionality is hs-plugins. Unfortunately hs-plugins doesn't work with the latest GHC release, 6.10.1.
Javier Neira

Monads in 15 minutes: Backtracking and Maybe - 0 views

  • type Choice a = [a] choose :: [a] -> Choice a choose xs = xs
  • Because Haskell doesn’t compute answers until we ask for them, we get the actual backtracking for free!
  • The missing function is almost too trivial to mention: Given a single value of type a, we need a convenient way to construct a value of type Choice a:
  • ...5 more annotations...
  • More math trivia: return is also known as unit and η. That’s a lot of names for a very simple idea.)
  • makePairs = choose [1,2,3] >>= (\x -> choose [4,5,6] >>= (\y -> return (x,y)))
  • makePairs' = do x <- choose [1,2,3] y <- choose [4,5,6] return (x,y)
  • Every monad has three pieces: return, map and join.
  • Backtracking: The lazy way to code
Javier Neira

99 questions/1 to 10 - HaskellWiki - 0 views

  • data NestedList a = Elem a | List [NestedList a]   flatten :: NestedList a -> [a] flatten (Elem x) = [x] flatten (List x) = concatMap flatten x
  • compress :: Eq a => [a] -> [a] compress = map head . group We simply group equal values together (group), then take the head of each. Note that (with GHC) we must give an explicit type to compress otherwise we get:
1 - 10 of 10
Showing 20 items per page