by baby · 4y ago · 0 comments
Rule 5 of Rob Pike[1] says:
> Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.
which makes a lot of sense to me. You see this concept being used all over the place in Golang/Rust.
But functional languages make you think in very different ways, which makes your program organized in much different ways as well. Although I can't quite put my finger on what exactly is so different.
Agda[1] and Idris[2] turn this idea into interactive editing with typed holes. You put a "?" in the code and the compiler fills the hole in for you based on the type that it infers for it. Haskell has a less expressive type system but it also has typed holes and Wingman[3] extends the Haskell Language Server to use them for interactive editing.
[1] https://www.youtube.com/watch?v=bRudW0aBNg8
Is this true? I understand types are abstract, while data structures are concrete.
Like, the type...
x -> a x y -> z y
...tells me nothing. But that is, among many things, the type for a hash map lookup. Still, a hash map is a particular implementation, and the type alone doesn't capture this – it could mislead me into thinking any implementation that fits this shape is fine.
This is the actual type of Haskell's dictionary lookup:
k -> Map k a -> Maybe a
I don't know about you, but that's pretty self-explanatory to me.
That's only because you, the programmer, expect a type named "hash map" to implement a hash map – but this information is not being described by the type itself, it's out-of-band. The actual definition of the hash map data structure is given by the algorithm. In fact, without inspecting the algorithm, it's impossible to know it correctly implements a hash map, you're just trusting the arbitrary name of the type.
> x -> a x y -> z y
> ...tells me nothing.
I am guessing that you wouldn’t see a type signature like this in a real codebase. The type signature of Data.HashMap.Strict.lookup is
(Eq k, Hashable k) => k -> HashMap k v -> Maybe v
which is quite a bit more readable. You can even search Hoogle for x -> HashMap x y -> y and find it, try it!https://hoogle.haskell.org/?hoogle=x%20-%3E%20HashMap%20x%20...
Not really. Types include datastructures. They define the set of values that a datastructure can assume. They do the same for everything else, like variables, functions etc.
From what I've seen, this is where people usually struggle when it comes to learning functional programming. People are so wrapped up in the algorithms that they have a hard time thinking in data in the way that is required to do functional programming effectively.
Really, in practice this means data is a partially applied, curried, identity function. Put something in, get the same thing out later, acting as memoization.
Functions are just boring implementation detail. The structures tell you everything.