Skip to main content

Home/ Haskell/ Group items tagged map

Rss Feed Group items tagged

Javier Neira

Haskell: The Confusing Parts - 0 views

  • f $ x = f x(f . g) x = f (g x)-- Some quick examples of using these. The following line...putStrLn (take 12 (map foo (bar ++ "ack")))-- ...can be rewritten as...putStrLn $ take 12 $ map foo $ bar ++ "ack"(putStrLn . take 12 . map foo) (bar ++ "ack")putStrLn . take 12 . map foo $ bar ++ "ack" In terms of good Haskell style, the last example above is preferable to the others. By the way, note that ($) has the lowest precedence (zero) of any operator, so you can almost always use arbitrary syntax on either side of it.
  • -- Actual definitions:f $ x = f x(f . g) x = f (g x)-- Some quick examples of using these. The following line...putStrLn (take 12 (map foo (bar ++ "ack")))-- ...can be rewritten as...putStrLn $ take 12 $ map foo $ bar ++ "ack"(putStrLn . take 12 . map foo) (bar ++ "ack")putStrLn . take 12 . map foo $ bar ++ "ack"
  • putStrLn (take 12 (map foo (bar ++ "ack")))-- ...can be rewritten as...putStrLn $ take 12 $ map foo $ bar ++ "ack"(putStrLn . take 12 . map foo) (bar ++ "ack")putStrLn . take 12 . map foo $ bar ++ "ack"
  • ...7 more annotations...
  • putStrLn (take 12 (map foo (bar ++ "ack")))
  • anywhere you might write \x -> foo x [1..10], you should instead write flip foo [1..10], where flip is a standard Prelude function that flips the first two arguments of whatever function you give it
  • two arguments of whatever
  • anywhere you might write \x -> foo x [1..10], you should instead write flip foo [1..10], where flip is a standard Prelude function that flips the first
  • The curry and uncurry functions stand in for \f x y -> f (x, y) and \f (x, y) -> f x y, respectively.
  • The curry and uncurry functions stand in for \f x y -> f (x, y)
  • I will point out that return is, in fact, not a return statement. It’s a function, and an inappropriately named function, at that. Writing return () in your do block will not cause the function to return.
  •  
    If you're used to the C family of languages, or the closely related family of "scripting languages," Haskell's syntax (mainly) is a bit baffling at first. For some people, it can even seem like it's sneaking out from under you every time you think you understand it. This is sort of a FAQ for people who are new to Haskell, or scared away by its syntax.
Javier Neira

jaspervdj - Maybe, null, and boilerplate code - 0 views

  • A first goal they serve is as "unassigned variables". I will not cover that here, instead I will focus on another use: abnormal return values.
  • So, null is some sort of "Not found" error here. But wait - they could have used an Exception here!
  • We see a similar behavior here: when the object is found, we get a Just a, and when it is not found, we get Nothing - comparable to a null pointer.
  • ...5 more annotations...
  • So, while we can get a null back from a function, we should never give it to a function.
  • Now, if you have written a lot of Java code, you know that checking for null is a vital part of the job, and fragments like the one above are pretty common.
  • In Haskell, however, Maybe is also a monad - and monads can be used to prevent common patterns in code - in other words, they assist you in the D.R.Y.-principle2. We can therefore write the above snippet again using do-notation3.
  • getPathMonth :: Map String String -> Maybe MonthgetPathMonth m = do p <- lookup "path" m d <- parseDate p return $ getMonth d
  • he whole "check-for-null-and-short-circuit" behavior is defined by the Maybe monad -
  •  
    getPathMonth :: Map String String -> Maybe Month getPathMonth m = case M.lookup "path" m of Nothing -> Nothing (Just p) -> case parseDate p of Nothing -> Nothing (Just d) -> Just (getMonth d) getPathMonth :: Map String String -> Maybe Month getPathMonth m = do p <- parseDate p return $ getMonth d
Xiaobin Huang

'Cannot justify constraints in explicitly typed binding ', Hugs complained. - 10 views

Now I know, it should be 'permutation :: Eq a => [a]->[[a]]'. Xiaobin Huang wrote: > hi all, > > I'm new to Haskell. > > I wrote a permutation prog. > > permutation [] = [[]] > p...

Javier Neira

Monads as containers - HaskellWiki - 0 views

  • A monad is a container type together with a few methods defined on it.
  • all the elements which a monadic container holds at any one time must be the same type (it is homogeneous).
  • map (fmap), return and join,
  • ...15 more annotations...
  • map, (but called fmap in Haskell 98) actually comes from the definition of a functor
  • That is, if f is a functor, and we are given a function of type (a -&gt; b), and a container of type (f a), we can get a new container of type (f b). This is expressed in the type of fmap: fmap :: (Functor f) =&gt; (a -&gt; b) -&gt; f a -&gt; f b If you will give me a blueberry for each apple I give you (a -&gt; b), and I have a box of apples (f a), then I can get a box of blueberries (f b). Every monad is a functor.
  • The second method, return, is specific to monads. If m is a monad, then return takes an element of type a, and gives a container of type (m a) with that element in it. So, its type in Haskell is return :: (Monad m) =&gt; a -&gt; m a If I have an apple (a) then I can put it in a box (m a).
  • takes a container of containers m (m a), and combines them into one m a in some sensible fashion. Its Haskell type is join :: (Monad m) =&gt; m (m a) -&gt; m a
  • If I have a box of boxes of apples (m (m a)) then I can take the apples from each, and put them in a new box (m a).
  • bind or extend, which is commonly given the symbol (&gt;&gt;=)
  • liftM :: (Monad m) =&gt; (a -&gt; b) -&gt; m a -&gt; m b liftM f xs = xs &gt;&gt;= (return . f) -- take a container full of a's, to each, apply f, -- put the resulting value of type b in a new container, -- and then join all the containers together.
  • The function that does this for any monad in Haskell is called liftM -- it can be written in terms of return and bind as follows:
  • Well, in Haskell, IO is a monad.
  • Lists are most likely the simplest, most illustrative example
  • The reason that bind is so important is that it serves to chain computations on monadic containers together.
  • You might notice a similarity here between bind and function application or composition, and this is no coincidence.
  • What bind does is to take a container of type (m a) and a function of type (a -&gt; m b). It first maps the function over the container, (which would give an m (m b)) and then applies join to the result to get a container of type (m b). Its type and definition in Haskell is the
  • xs &gt;&gt;= f = join (fmap f xs)
  • bind (&gt;&gt;=)
Javier Neira

Monads in 15 minutes: Backtracking and Maybe - 0 views

  • type Choice a = [a] choose :: [a] -&gt; 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] &gt;&gt;= (\x -&gt; choose [4,5,6] &gt;&gt;= (\y -&gt; return (x,y)))
  • makePairs' = do x &lt;- choose [1,2,3] y &lt;- 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] &nbsp; flatten :: NestedList a -&gt; [a] flatten (Elem x) = [x] flatten (List x) = concatMap flatten x
  • compress :: Eq a =&gt; [a] -&gt; [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 - 7 of 7
Showing 20 items per page