r/dailyprogrammer • u/Coder_d00d 1 3 • Sep 09 '14
[Weekly #10] The Future
Weekly Topic:
Read enough blogs or forums and you can see the future. What trends or topics are coming down the line? Is it a new language? New design? New way to engineer software?
Last Week:
54
Upvotes
View all comments
Show parent comment
3
u/gfixler Sep 15 '14
I've been learning Haskell lately, and it's been eye-opening. I had a big bug in a program not long ago, and it turned out to be type-based, and I finally matured enough as a developer to see that clearly, and suddenly I felt a longing for the safety of types. I've turned my nose up at types for 23 years of dynamic programming in a couple dozen languages, but then Haskell came along. Now that they're based on some mathematical foundations, and not just how many bits can fit into something, some cool things are revealing themselves to me.
One of them is using type parameters in function notation, instead of concrete types. They actually tell you a lot. A function (Int -> Int), which takes an Int and returns an Int has an obvious shape, and an obvious set of abilities, but when you use a type parameter (lower case, typically
a,b, etc), you're saying the function has to work for any type, and the user gets to say which.The signature of a function is its type in Haskell. If you have a type (a -> a), which describes a function that takes something of any type, and returns something of that type, you actually know a lot. In fact, the only thing that this function can return is whatever it's given. For example, it can't return the negative of what it's given, because it could be given a Bool, or a String. If you see (a -> a), the only function it can be is the identity function. a -> a -> a is similarly constrained. The only thing that function can do is return the first or second argument.
Type signatures, i.e. the types of functions are so useful that there are competing search engines used by Haskellers that let you search by types, and it's actually remarkably useful to do so. If you ask a question in a Haskell forum, the first thing many responders will do is show you the type of the thing you're asking about, because it tells you so much about the function. I've always viewed types as an in-the-way nuisance, but - at least in Haskell - they seem to be very informative things.
For example, I might wonder how to split a string into words, so I search
String -> [String], because I know I'm starting with a String, and the result would be a list of Strings. The two most useful things (that I've used) are the first two results. Putting them back together? First two results :) I want to get the first n elements of a list. That would probably be a number of things to get, and then a list of something, and the result would be a [smaller] list of something, soInt -> [a] -> [a], and indeed,takeanddropare the first two results.