A good resource on the principles behind functional programming?

mattseh

import this
Apr 6, 2009
5,504
71
0
A ~= A
I know a bit, but I feel not enough. Not really interested in language specific stuff, but I feel a certain Texan is going to push that on me anyways :)
 


Start writing it... profit! I just sort of picked it up as I went, it's everywhere in javascript. So I guess, learn javascript.
 
dat map:

Code:
window.location = window.location.pathname+'?networks='+$("input[type='checkbox']:checked").map(function () { return this.value; }).get().join(',');

Rage9, thanks, I've explored a lot of python functional stuff, and have a decent amount of experience with JS functional stuff, but I have no idea what a monad is, for example, a good guide to the terminology would be good.
 
dat map:

Code:
window.location = window.location.pathname+'?networks='+$("input[type='checkbox']:checked").map(function () { return this.value; }).get().join(',');

Rage9, thanks, I've explored a lot of python functional stuff, and have a decent amount of experience with JS functional stuff, but I have no idea what a monad is, for example, a good guide to the terminology would be good.

As best I can tell, and this is ultra nerd functional stuff, best explanation I've found:

So what is a monad? Well, it’s a design pattern. It says that whenever you have a class of functions that accept one type of thing and return another type of thing, there are two functions that can be applied across this class to make them composable:

There is a bind function that transforms any function so that accepts the same type as it returns, making it composable
There is a unit function that wraps a value in the type accepted by the composable functions.
 
If all you're after are the principles then this video is one of the best I've seen in explaining them without over complicating nor over simplifying:
Brian Beckman: Don't fear the Monad - YouTube

In my experience, I've grown to not believe the hype. Side effects are integral to writing useful applications and the stricter FP languages make working with them harder than they need to be generally. Although, I believe Dr. Beckman gets it right in the video when he asserts that FP is about managing complexity.

I definitely prefer to attack any computationally complex problems with a functional style. The immediacy of being able to utilize function composition for code reuse and to quickly build up abstractions with simple interfaces is great. Adhering to stricter type signature definitions makes refactoring anything a breeze, since we can swap any and all functions that share the same signature with complete confidence.

Once a solution takes shape, I'll generally wrap this functional core in an imperative shell to make it nice and easy to call into the service and expecting something uniform in return.
 
The main principle is "no side effects". A function must return the same results for the same arguments. No side effects, immutable data, all state you work with contains in function arguments. No shared state and deterministic functions eliminate a whole load of bugs and problems. Your function transforms this state and returns if for further transform. In real life there are side effects: when you do IO (Haskell) or when you send a message to another green process (Erlang), but these effects are highly localized.

I don't recommend python or js even if they have some functional features as these languages have global variables and do not have pattern matching. Try to write a simple app with Haskell (more or less academic research language) or Erlang (braindead simple functional language with emphasis on concurrent applications, battle tested in such companies as Ericsson or WatsApp), you'll see the difference. You might try Scala if you like JVM, but imo this language is overcomplicated, it's like C++ of functional languages.
 
Write a tic-tac-toe AI in Haskell and Scheme. That should be enough to give you the basics.
 
The main principle is "no side effects". A function must return the same results for the same arguments. No side effects, immutable data, all state you work with contains in function arguments. No shared state and deterministic functions eliminate a whole load of bugs and problems. Your function transforms this state and returns if for further transform. In real life there are side effects: when you do IO (Haskell) or when you send a message to another green process (Erlang), but these effects are highly localized.

I don't recommend python or js even if they have some functional features as these languages have global variables and do not have pattern matching. Try to write a simple app with Haskell (more or less academic research language) or Erlang (braindead simple functional language with emphasis on concurrent applications, battle tested in such companies as Ericsson or WatsApp), you'll see the difference. You might try Scala if you like JVM, but imo this language is overcomplicated, it's like C++ of functional languages.

It seems like "no side effects" should be the norm for well written code in any language. Is it just that functional languages force it?
 
It seems like "no side effects" should be the norm for well written code in any language. Is it just that functional languages force it?
In OO languages methods change objects properties. These are the side effects I was talking about, not just global variables. When you debug a function in a functional language and print function input, you can be sure that this is the whole data your function works with, as you just cannot access other things that could be changed from outside.

Plus immutability - you can not have immutable data in traditional languages, as for example in loops you have no choice but mutate index variable. In functional languages you have variables in math sense - once you assign a value to it you cannot change it any more. For example, a loop in functional language is made either by lists functions (like foldl) or like this (erlang syntax, function sum(N), returns sum from 0 to N):
Code:
sum(N) when is_integer(N) andalso N >= 0 ->
   sum(N, 0).

sum(0, Acc) ->
  Acc;
sum(N, Acc) ->
  sum(N-1, Acc + N).
There's also neat things from functional languages like pattern matching and first-class functions/closures. First-class functions are already found their way to mainstream languages (lambdas), pattern matching is not yet, but believe me, it is very elegant powerful thing.

But again, the core of functional programming is no side effects and immutability of data.