I've recently picked up Haskell and I'm trying writing a simple program to sort acronyms. Basically, I have a list of acronyms - each acronym is a pair of Strings, for example ("DH", "Diffie-Hellman") - and I want to sort them and print them in the following format (Acronym, Meaning, Index).
For example, if
acronyms = [("ECDLP", "elliptic curve discrete logarithm problem"),
("DH", "Diffie-Hellman"),
("KDF", "key derivation function")]
then the output should be
("DH","Diffie-Hellman",1)
("ECDLP","Elliptic Curve Discrete Logarithm Problem",2)
("KDF","Key Derivation Function",3)
Currently, the code I have is the following.
main :: IO ()
main =
mapM_
print
(zipWith
(curry (\((a, b), c) -> (a, b, c)))
(sort . nub $ map (capitalise <$>) acronyms)
[1 ..])
Here capitalise is a function that I wrote that capitalises the first character of each word in a string. Overall, this program is working but I would like to get some feedback on how I can improve it. Finally, is there any way I could make this curry (\((a, b), c) -> (a, b, c)) part in the pointfree style?
Thanks
I would start by making it less point-free! That
curryis a mess, and accomplishing nothing. Just write a lambda that combines a tuple with an index directly.This is fine, but I think the fmap instance for tuples is surprising enough that, in code I wanted anyone else to read, I'd name that function too:
You could also be more modern, and use
traverse_instead ofmapM_. I'm sympathetic to the clingers-on ofmapM_, though - the name is more obviously indicative of what it does.