View difference between Paste ID: YHci9g6N and y3Nhn3ce
SHOW: | | - or go back to the newest paste.
1-
Regarding your uncertainties in Episode 138 about functional programming, I think the problem is that you're reading the Wikipedia page instead of jumping in and experiencing it. This gives you the math nerd / pure CS approach, which sets your head to spinning with lambdas and such.
1+
Regarding your uncertainties about functional programming in Embedded episode 138, I think the problem is that you're reading the Wikipedia page instead of jumping in and experiencing it. This gives you the math nerd / pure CS approach, which sets your head to spinning with lambdas and such. That's like teaching someone electronics by starting them out on Maxwell's equations instead of blinking an LED.
2
3-
There's actually a whole spectrum of FP languages. I've written up a concise summary here:
3+
There's actually a whole spectrum of FP languages which spans a much wider gamut than the pure lambda calculus viewpoint of FP. I've written up a concise summary here:
4
5-
  http://stackoverflow.com/questions/2271417/is-erlang-really-a-functional-language/11111614#11111614
5+
    http://stackoverflow.com/questions/2271417/is-erlang-really-a-functional-language/11111614#11111614
6
7
That's an answer to a question about Erlang, but it applies broadly to FP as a whole.
8
9-
Notice that JavaScript -- a language both of you know, I believe -- is "functional" by my definition. In fact, JavaScript was originally created as a C-flavored Scheme dialect. That's why you can say something like this in JavaScript:
9+
Notice that JavaScript -- a language both of you know, I believe -- is "functional" by my definition. In fact, JavaScript was originally created as a C++-flavored Scheme dialect. That's why you can say something like this in JavaScript:
10
11-
  var f = function(arg1, arg2) {
11+
    var f = function(arg1, arg2) {
12-
     return function(arg3) {
12+
        return function(arg3) {
13-
         // Do something with arg1, arg2, and arg3
13+
            // Do something with arg1, arg2, and arg3
14-
     };
14+
        };
15-
  };
15+
    };
16
17
That assigns a 2-parameter function to a variable, which you can call as f(a, b). When you do that, you get back a different function that takes one parameter, but which has arg1 and arg2 available to it because of a common feature of FP languages called closure. This is powerful stuff.
18
19
It might be clearer with a concrete example:
20
21-
   var animals = [
21+
    var animals = [
22-
      { name: 'kangaroo',
22+
       { name: 'kangaroo',
23-
        weight: 110
23+
         weight: 110,
24-
      },
24+
       },
25-
      { name: 'ox',
25+
       { name: 'ox',
26-
        weight: 643,
26+
         weight: 643,
27-
     },
27+
       },
28-
     { name: 'mouse',
28+
       { name: 'mouse',
29-
       weight: 4
29+
         weight: 4,
30-
     }
30+
       },
31-
   };
31+
    ];
32-
   var sorted = animals.sort(function(a, b) { return a.weight - b.weight });
32+
    var sorted = animals.sort(function(a, b) { return a.weight - b.weight });
33
34-
That sorts the animals by weight. I didn't have to write a separate function and pass it in via a function pointer as with C's sort(), I just built a function value and passed it in. 
34+
That sorts the animals by weight. I didn't have to write a separate function and pass it in via a function pointer as with C's sort(), I just built a function value inline and passed it in.
35
36-
I think the best way to learn functional JavaScript is to build a dynamic web app using jQuery, which is programmed in a highly FP style.
36+
Functions in JavaScript are *values*, not a distinct entity class from variables and objects. These values are of class Function and they have properties like any other JavaScript object. For example, the f function value above has f.length which is equal to the number of parameters the function expects, 2 in this case. If you say f().length instead, you get 1, since that's the length of the parameter list for the inner function.
37
38
For more surprising details on Function in JavaScript, see:
39
40
    https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function
41
42-
For example, an if/else statement in F#/OCaml always returns a value. A simple if/else statement in F# actually works more like C's ternary operator than if/else in traditional imperative languages. But unlike C, when you need more than two branches, the required nesting and chaining is generally a lot easier to read:
42+
I think the best way to learn functional JavaScript is to build a dynamic web app using jQuery. For example:
43
44
    $("div.commentForm").mouseenter(function(event) {
45
        alert("The mouse entered comment form ID " + event.target.id + "!");
46
    });
47
48-
   let x = if someFunction(param1, param2) then
48+
In this example, the $ function returns a jQuery object containing a list of matching objects to which we attach an event handler. Function call chains, applications of functions to lists of objects, and inline function values (a.k.a. lambdas) are all FP concepts.
49
50-
             elseif otherFunction(param3) then
50+
51
52-
             else
52+
53
54
For example, an if/else statement in F#/OCaml always returns a value. It actually works more like C's ternary operator than if/else in traditional imperative languages. But unlike C, when you need more than two branches the required nesting and chaining is generally a lot easier to read:
55
56
    int x = someFunction(param1, param2) ? 42 : otherFunction(param3) ? 69 : 99;
57-
   if someFunction(param1, param2):
57+
58-
       x = 42
58+
59-
   elif otherFunction(param3):
59+
60
    let x = if someFunction(param1, param2) then
61-
    else:
61+
62-
       x = 99
62+
            elseif otherFunction(param3) then
63
                69
64-
Maybe that looks clearer to you, but there are problems. First, you've repeated the variable name three times. Second, there's nothing forcing you to return a value from all three branches, so you could miss one, leaving x undefined. There's no such thing as an undefined value in a strong FP language like F#. (And thus, no null or uninitialized pointer dereferences!)      
64+
            else
65
                99
66-
Getting back to my F# example above, x is a read-only value, not a variable, and it is equal to the value of the last statement in each branch. This means you can't have a dangling "else" branch. (Well, that's not entirely true, but that gets us off into the weeds...)  This closes off a large class of programming errors.
66+
67
Notice that F# is whitespace-sensitive and doesn't have semicolon statement separators, much like Python. But to say the same thing in Python, you'd have to do it this way, because an if/elif/else statement doesn't have a value:
68
69
    if someFunction(param1, param2):
70-
Because F# is a .NET language, anywhere you could use VB.net or C#, you can use F#.
70+
        x = 42
71
    elif otherFunction(param3):
72
        x = 69
73
     else:
74-
If you need compiled binaries, you can't cope with the .NET runtime, or you're anti-Microsoft, you might prefer OCaml. But, IMHO, F# is OCaml with all the ugly bits sanded off. :)
74+
        x = 99
75
76-
By all means, stay away from Haskell, Lisp, and Scheme. That's the pure CS nerds off doing their thing. Erlang and the ML family are far more practical languages for practitioners like ourselves.
76+
Maybe that looks clearer to you, but there are problems. First, you've repeated the variable name three times, a violation of the DRY principle. Second, there's nothing forcing you to return a value from all three branches, so you could miss one in a more complicated bit of code, leaving x undefined. There's no such thing as an undefined value in a strong FP language like F#. (And thus, no null or uninitialized pointer dereferences!)      
77
78
Getting back to my F# example above, x is a read-only value, not a variable, and it is equal to the value of the last statement in each branch. (We're just returning a simple integer in the example, but it could be code of any complexity.)
79
80
By refusing to let you have a dangling "else" branch, F# closes off a whole class of programming errors. (Well, there are cases where "else" is not required, but chasing such details will send us off into the weeds.)
81
82
I wrote a long post about why I like F# here: http://goo.gl/0yfTfR
83
84
Because F# is a .NET language, you can use it anywhere you can use VB.net or C#.
85
86
F# is also portable to non-Windows systems via Mono. I've used it on Linux and Mac OS X, even doing nontrivial things like UDP multicast networking code in it.
87
88
You might prefer OCaml to F# when:
89
90
- you need native compiled binaries rather than .NET IL code;
91
- you can't accept use of the .NET or Mono runtime; or
92
- you're anti-Microsoft
93
94
Outside of those cases, I prefer F#.  It's like OCaml with all the ugly bits sanded off. :)
95
96
By all means, stay away from Haskell and Lisp. That's the pure CS nerds off doing their thing. Erlang and the ML family are far more practical languages for practitioners like ourselves.
97
98
I'm told that Scala fills that role for JVM users, but I couldn't say from personal experience.
99
100
Apple's new Swift programming language also has quite a lot of the FP nature to it.