I've been following the Learn You a Haskell guide. Now I am in the Modules chapter, where it presents a ton of useful functions from different modules. Some Data.List module functions were just enough to boggle my mind. It is really insane how expressive the Haskell language can be and at the same time simple, despite the fact I need to spend a considerable amount of time trying to understand some of the functions.
ghci> let xs = [[5,4,5,4,4],[1,2,3],[3,5,4,3],[],[2],[2,2]] ghci> sortBy (compare `on` length) xs [[],[2],[2,2],[1,2,3],[3,5,4,3],[5,4,5,4,4]]
The snippet above (as the author says) is really like reading English!
Reading the article I wondered how the implementation of isInfixOf function would be, then I searched it and I found the snippet beneath:
isInfixOf :: (Eq a) => [a] -> [a] -> Bool isInfixOf needle haystack = any (isPrefixOf needle) (tails haystack)
Incredibly beautiful and simple, right? It still fries my brain anyway.
Whenever I try to understand what a function actually does, I check its type definition and I keep hammering it into my brain until it somehow starts make sense.
That's it. Nothing really great about this post. I just wanted to share some feelings I've been getting from functional programming.
submitted by /u/Mark_1802
[link] [comments]