Skip to content

Instantly share code, notes, and snippets.

@hughrawlinson
Last active February 11, 2016 11:39
Show Gist options
  • Save hughrawlinson/6b8237f8c0a5e7f7d8ce to your computer and use it in GitHub Desktop.
Save hughrawlinson/6b8237f8c0a5e7f7d8ce to your computer and use it in GitHub Desktop.
A Brief Summary of Haskell

A Brief Summary of Haskell

Haskell is a functional programming language, in the ML family of languages, inheriting many of it’s properties from Lambda calculus. The label ‘functional’ refers to the fact that in functional languages, functions are treated as higher order objects. This means that they may be assigned to variables in the functional languages that have a concept of ‘variables’ in the traditional sense, and are otherwise treated as values. Both Haskell and Javascript are ‘functional’ languages in this capacity, however, they differ in many other ways. Javascript is a loosely typed language, meaning that it’s values, expressions, and functions do not have a specific type. One function could return values of different types depending on the arguments it receives, external state, or potentially other factors like internal calls to type unsafe APIs or underlying libraries. Javascript also features type coercion, where the interpreter will automatically convert variables of certain types into other types when operations are performed on them in order to run comparison functions between the two. For example, in Javascript the code “1 == ‘1’” returns ‘true’, as the string ‘1’ is converted into a Number prior to the comparison. This introduces the concept of ‘strict equality’, an equality operation that checks for type equality as well as value equality. The ‘strict equality’ operator is ‘===‘, and the code “1 === ‘1’” returns ‘false’, as the types are not equal. Haskell, on the other hand, has a strict type checker. Each expression, value, and function has a type signatures. This enables the compiler to check for type compatibility in programs, and throw a type error at compile time rather than in the runtime. This removes a whole class of errors that have the potential to occur during execution and cause problems in production software. In addition to the static type checker, Haskell enforces ‘purity’ on its functions. Functional purity has two requirements; the function must return the same value for any given arguments every time it is called with those arguments, and calls to the function must not result in any changes in state external to that function. Purity enforcement has the effect of removing bugs related to state, such as references to variables that have been removed by garbage collection, or by another impure function which then go on to cause bugs. This is particularly advantageous in horizontally scaled web application architectures, where consecutive requests by a single client to a service that contains in-memory state would have to always go to the same server, which would have a hugely detrimental impact on load-balancing. Removing references to machine specific in-memory state in a server-side web application would permit load-balancing techniques that can spread load more evenly across a set of servers (the ‘round-robin’ load balancing technique is an example of this) as there is no need for a server to reference variables that may be stored in another server’s runtime. Haskell does still permit stateful operations such as input and output, networking operations, or calls to external APIs or hardware with a development pattern called a ‘monad’. Monads separate operations that involve side-effects from the rest of the runtime, so that the rest of the function can still have the guarantees with regard to type safety and purity that were mentioned earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment