# News aggregator

### foobarcode.me

### Roman Cheplyaka: Recognizing lists

You are given the following Haskell data type:

data T = A Int | B StringYour task is to write a function

recognize :: [T] -> Boolthat would accept a list iff it contains exactly three elements, of which:

- one is an A with a value between 3 and 7
- one is an A with an even value
- one is a B with a value ending in .hs

The A elements mentioned in conditions (1) and (2) have to be distinct, so [A 4, A 17, B "Prelude.hs"] should not be accepted, even though A 4 satisfies both (1) and (2).

This problem often arises in blackbox testing. You do something to a system; you wait for some time while gathering all events that the system generates. Then you want to check that the system generated all the events it had to generate, and no extra ones. However, the order of events may be non-deterministic. If the system concurrently makes a web request, updates a database and sends a message to a message bus, the order in which these events will be observed by your test suite is arbitrary.

A similar problem arises in input validation.

InterfaceWe want to solve this problem in a general and scalable way. What would an interface to such solution look like? There are two principal options.

One is a simple function

recognize :: [t -> Bool] -> [t] -> Boolthat takes individual predicates and combines them into a recognizer.

Another is to split the process into the *build* and *use* phases by the means of an intermediate data type:

In this approach, you convert individual recognizers to monoidal values, combine those values and extract the result.

Semantically, the two are equivalent: if you have one of these interfaces available, you can implement the other one. In a way, this is a consequence of lists being the free monoid, but writing this down explicitly is a good exercise nevertheless.

The advantages of a *build-use* interface are:

- When the cost of the
*build*phase is non-trivial, it can be amortized over multiple uses and/or incremental builds. (This won’t be the case here.) - The use of a specialized type is generally good for the type system hygiene.

We’ll start with the *build-use* interface because I find it more instructive.

To build the monoidal parser, we’ll use the action-permutations library. You may remember it from the article on JSON parsing.

newtype Parser t = Parser { getParser :: Perms (StateT [t] Maybe) () }StateT [t] Maybe a is the familiar type of parser combinators, [t] -> Maybe (a, [t]), that return the left-over input. We wrap it in Perms to achieve the order independence, and then newtype it to convert its Applicative instance to a Monoid one:

instance Monoid (Parser t) where mempty = Parser (pure ()) mappend (Parser a) (Parser b) = Parser $ a *> bThe build and use functions are straightforward:

toParser :: (t -> Bool) -> Parser t toParser isX = Parser $ Perm.atom $ StateT $ \ts -> case ts of (t:ts') | isX t -> Just ((), ts') _ -> Nothing runParser :: Parser t -> [t] -> Bool runParser p ts = case (flip execStateT ts . runPerms . getParser) p of Just [] -> True _ -> False UsageFirst, let’s express our conditions in Haskell code.

isA1, isA2, isB :: T -> Bool isA1 x | A n <- x, 3 <= n, n <= 7 = True | otherwise = False isA2 x | A n <- x, even n = True | otherwise = False isB x | B s <- x, ".hs" `isSuffixOf` s = True | otherwise = FalseCombine them together:

parser :: Parser T parser = (mconcat . map toParser) [isA1, isA2, isB](we could replace mconcat . map toParser with foldMap toParser from Data.Foldable)

recognize :: [T] -> Bool recognize = runParser parserNow try it out:

ex1 = recognize [A 4, A 17, B "Prelude.hs"] -- => False ex2 = recognize [A 4, A 18, B "Prelude.hs"] -- => True ex3 = recognize [B "Prelude.hs", A 18, A 4] -- => True ex4 = recognize [B "Prelude.hs", A 18, A 4, A 1] -- => False### JP Moresmau: EclipseFP 2.6.4 released!

A couple of fixes and small enhancements have been made, the release note are here.

Just update by pointing your Eclipse update feature to http://eclipsefp.sf.net/updates.

Happy Haskell Hacking!

### Why QuickCheck can be useful, from the perspective of Test-Driven Development

### Tranversable instance for Trie

Hey!

I've got the following data type for Trie:

data Trie a = Trie { value :: Maybe a , children :: M.Map Char (Trie a) } deriving (Show)How would I go about making a transversable instance for this data type? So far I have:

instance Traversable Trie where traverse f t | isJust (value t) = Trie {value = f (value t), children = traverse (traverse f) (children t)} | otherwise = Trie {value = Nothing, children = traverse (traverse f) (children t)}Which is not working and I get the error:

C:\trie.hs:24:82: Couldn't match type `M.Map Char' with `Trie' Expected type: Trie (Trie a) Actual type: M.Map Char (Trie a) In the second argument of `traverse', namely `(children t)' In the `children' field of a record C:\Users\Matias\Downloads\trie.hs:26:78: Couldn't match type `M.Map Char' with `Trie' Expected type: Trie (Trie a) Actual type: M.Map Char (Trie a) In the second argument of `traverse', namely `(children t)' In the `children' field of a record Failed, modules loaded: none. submitted by GrimBeast[link] [6 comments]

### Any Haskell enthusiasts from Bangalore India?

I am a newbie to Haskell programming. I would like to know if there are any Haskell enthusiasts that would like to work on Haskell projects. My focus is currently on learning Haskell and eventually develop side projects in Haskell.

submitted by vrbala[link] [11 comments]

### www.macs.hw.ac.uk

### Pure functional and pure logical language at thesame time

### Haskell Project Ideas?

Hey everyone! First time post on here, and I have a request that hopefully you guys can help me with. I'm currently in a programming language studies class, where we are having to pick a language, learn it, and develop some sort of project within around 3 months during the semester. I chose haskell because it seemed like the most interesting, having never worked in a functional language before.

My request is if you guys could throw out some ideas for me! Not gonna lie, I am not the most creative guy when it comes to school projects, so hopefully someone here can point me in the right direction! The project does not need to be 100% haskell, but it does need to be focused primarily around the use of it. Any and all help will be greatly appreciated!

submitted by Endeejay[link] [10 comments]

### Need to understand "Type Constructor"

In LYAH (http://learnyouahaskell.com/making-our-own-types-and-typeclasses#type-parameters), while citing an example of type constructors, **Maybe** is referred to as type-constructor.

If I follow definition, given in the same section for type-parameters, **Just**, seems to be the type-constructor. For example, in Just "Hello" - Just takes a String and returns a "Maybe [Char]".

Can anyone explain, which one is the type-constructor **Maybe a** or **Just** and why?

[link] [15 comments]