2
\$\begingroup\$

I'm learning Haskell on my own, and I'm wondering if this is idiomatic Haskell.

In particular: is it a common pattern to "wrap" the result in a list in order to make a function total? (that's what I ended up doing to ensure the function was total and no compiler warnings about not matching a pattern).

safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
firstLetters :: [String] -> [Char]
firstLetters ss = concat $
 map (\s -> case safeHead s of
 Nothing -> []
 Just c -> [c]) ss
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Nov 20, 2014 at 5:01
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

Well a few thoughts, of all String = [Char] so we could really give the signature

firstLetters :: [String] -> String

Whether you think this is better is a matter of taste. Second, you should take a look at

Data.Maybe.mapMaybe :: (a -> Maybe b) -> [a] -> [b]

It's exactly what you'd expect, it applies a function to Maybes and prunes out the Nothings. This simplifies the slightly ugly concat bit.

 firstLetters = mapMaybe safeHead

Much clearer :) In the interest of teaching you to fish, here's how I found the name of mapMaybe

answered Nov 20, 2014 at 5:10
\$\endgroup\$
2
  • \$\begingroup\$ When you say "is a matter of taste" is that a 50/50 divide in the Haskell community, or more like a 90/10 towards using String? \$\endgroup\$ Commented Nov 20, 2014 at 5:24
  • \$\begingroup\$ @j-a 50/50. In general you should prefer String when you're using well strings. In all other cases it's entirely up to you! \$\endgroup\$ Commented Nov 20, 2014 at 6:02
3
\$\begingroup\$

In addition to @jozefg's answer, Data.Maybe also contains listToMaybe.

listToMaybe :: [a] -> Maybe a

The listToMaybe function returns Nothing on an empty list or Just a where a is the first element of the list.

Which reduces the code to

firstLetters = mapMaybe listToMaybe
answered Nov 20, 2014 at 5:25
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.