# Bay Area Haskell Users Group

• Mar 23, 2014 · 2:00 PM
• This location is shown only to members

Ninety-Nine Haskell Problems is a great place to start gaining familiarity with the Haskell language through basic coding drills and group problem solving.

Let's get together and go through the first several questions as a group.  We can take turns walking through solutions, and learn from our different approaches, styles, and techniques as we go.

NOTE: Depending on the level of interest, we might switch venues to accommodate more attendees.

• ##### Eugene S.

Hi guys,

I just tried to wrap my head around the problem #7 (aka "flatten a nested list") that was discussed a lot because I still don't like the solution found even in the Answers section.

I think it's possible to implement it without concatMap or (++) or using list reversing:

data NestedList a = Elem a | List [NestedList a]

get1st :: NestedList a -> (Maybe a, Maybe (NestedList a))
get1st (Elem e) = (Just e, Nothing)
get1st (List []) = (Nothing, Nothing)
get1st (List (l : ls)) = let (e, l') = get1st l
tail = maybe (List ls)
(\h -> List (h : ls)) l'
in maybe (get1st tail)
(\e' -> (Just e', Just tail)) e

flatten :: NestedList a -> [a]
flatten nl = let (a1, as) = get1st nl
in maybe [] (: maybe [] flatten as) a1

main = putStrLn . show . (take 100) . flatten \$
List [List [List [], Elem 4], List [Elem 3], Elem 2]

March 23, 2014

• ##### jay b.

Here's a simpler version with your same goal of not using '++' or 'concatMap':

It uses a function with an accumulator and flattens in reverse order.

1 · April 1, 2014

• ##### Eugene S.

Hi Jay,

Your solution is likely the most elegant but it doesn't work for infinite input. It's not lazy in its input parameter. Just try the following:

main = putStrLn . show . (take 10) . flatten \$
List \$ map Elem [1..]

The proper output is:
[1,2,3,4,5,6,7,8,9,10]

:)

April 2, 2014

• ##### l g

Hey, can't seem to get in the building.

March 23, 2014

### 20 went

• ##### Eugene S.
• A former member
• A former member
• A former member
• A former member

### San Francisco, CA

Founded Apr 26, 2011

#### People in this Meetup are also in:

• ##### The SF JavaScript Meetup

12,225 JS Hackers

• ##### Ember.js San Francisco

2,247 Developers

• ##### ReactJS San Francisco Bay Area meetup

4,778 Developers

• ##### SF Data Mining

12,098 Data Scientists

• ##### SF Data Science

11,748 Data Scientists

• ##### React Native San Francisco

2,970 Developers