Haskell One Liners to Impress Your Friends
blog.fogus.me
Haskell One Liners to Impress Your Friends
1–10 of 19 posts
Re: Haskell One Liners to Impress Your Friends
#2fibs = 1 : scanl (+) 1 fibs
Re: Haskell One Liners to Impress Your Friends
#3My personal favourite Haskell one-liner is using scan for the fibonacci sequence: fibs = 1 : scanl (+) 1 fibs
Re: Haskell One Liners to Impress Your Friends
#4My personal favourite Haskell one-liner is using scan for the fibonacci sequence: fibs = 1 : scanl (+) 1 fibs
Could you explain what it does?
So the first fibonacci number is 1 (we're concatenating it onto the front of the list), the second fibonacci number is also one (the second argument to scan), and every fibonacci number after that is the sum of the previous number (scan's running total) and the one before that.
Re: Haskell One Liners to Impress Your Friends
#5They require you to actually have data in Haskell structures, and those that don't aren't one-liners.
Re: Haskell One Liners to Impress Your Friends
#6First, consider that 6 of them are essentially applying a builtin function. That you can sum a list with the function
sum [1..10]
is not impressive, it at best speaks to what functions the language designers thought to include. Similarly, partition (>60) [49, 58, 76, 82, 88, 90]
might be impressive to someone who has never seen first class functions (though they'd probably ascribe it to syntactic tricks), I think most programmers who are considering learning Haskell would say "Oh, hey, there's a builtin partition function. How nice."On the other hand, there would be a lot of worrying features from these examples. Consider
let fileLines = fmap lines $ readFile "data.txt"
What is fmap and how is it similar or different from map? Why not just "lines $ readFile ..." (assuming you find $ notation readable, which you'd probably pick up on fairly quickly)? Why, in the birthday example, do we use mapM_? How are map, fmap, mapM_, and presumably some hypothetical mapM, related? because the use of mapM_ seems identical to map, and fmap looks completely unrelated. What is foldl1, and how is it different from foldl?In the same vein, why are there liftM calls all over that one example which says "I have curl and xml parsing as libraries"? Or, in the alternate form of that example, what is and how is it different from ? Because it looks the same, but not quite, since it forces the left $ to become ., not ...
Overall, the only one of these that would have looked interesting to me two years ago, when I programmed exclusively in Python, would be the last two, and even so, the last one mostly looks like, "hey, we have list comprehensions that are better somehow" and the one before that looks like "hey, we have a concurrency library".
What makes a good advertisement for a language? "I have the following functions built in" doesn't work. You need to demonstrate some core language features that work in a large number of cases. Yes, you have a "sum" function. But that tells me nothing if I want a standard deviation, unless you have that built in too. If, instead, you demonstrated a list comprehension, I might say, "Oh hey, I could use that pattern a lot". If I'm advertising Python, I might show off the fact that for loops loop over iterators, that objects are dynamically typed (with, say, file-like objects), decorators, and other examples where the language provides general structure to build upon.
So if you want to advertise Haskell, why not show some simple monad in action that stresses that it's a general pattern of abstracting computation (or, say, demonstrate writing your own data structure and then using fmap on it), show an infinite list, maybe note that you can transparently compile in concurrency. These aren't well-thought-out suggestions, but look, the Fibonacci example is something that might excite a Python (say) or Java programmer (I remember first reading about generators and being astounded!) far more than "Look, XML parsing and Curl in my libraries! Ignore the liftMs!"
Point being, if the function is already written, any operation is a one-liner. So you better either be claiming that all functions have been written in your language (the "batteries included" ad slogan) or that the language provides some way of building up its few functions to do whatever you want, easily. "Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary," they say. This blog post shows me 10 features, and also what appear to be quite a few structural weaknesses. I'm not impressed.
Re: Haskell One Liners to Impress Your Friends
#7My personal favourite Haskell one-liner is using scan for the fibonacci sequence: fibs = 1 : scanl (+) 1 fibs
> fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
Which IMO is easier to grasp intuitively.
Re: Haskell One Liners to Impress Your Friends
#8 powerset = filterM $ const [True, False]Re: Haskell One Liners to Impress Your Friends
#9Re: Haskell One Liners to Impress Your Friends
#10Even shorter: any (`isInfixOf`tweet) wordlist
Partial application of infix operators automatically adds flip if needed.