5 ms·
It's not functional programming if you don't have nested functions and/or closures, is it?
by wlievens 17y ago
It's not functional programming if you don't have nested functions and/or closures, is it?
- ubernostrum 17y ago"Functional programming" is one of those terms that's so undefined (and, really, indefinable) that it basically boils down to "I'll define it to mean that what I'm doing fits and what you're doing doesn't".
- loup-vaillant 17y agoI think most "functional programmers" will agree on this definition: avoid (side) effects, or at least try to isolate them from the rest of your program. It doesn't mention functions at all, which can confuse programmers unfamiliar with Lisp, ML, or Haskell. However, when you actually program with such restrictions, your only choice is closures or insanity.
- jerf 17y ago"Most" functional programmers might agree that is a component, but that it is missing many other critical elements of a useful definition. Immutability is a recent one, for instance. Then you get people like me who observe there really are two "functional"s: There's an old one that simply means functions are first-class objects and usually means you get closures. This is the definition whereby Lisp is the canonical functional language. Then there's the more modern definition that involves immutable variables, no side effects or carefully-isolated side-effects, all of the older definition's closures and such, and possibly a few other things depending on who you talk to. Note by this definition Lisp is no longer "functional". C being C, it can do the older functional style with some manually-hacked-in closures, at which point you can squint and call it an old-style-functional language. Certainly more so than a Basic with no ability to have a function pointer at all, though lacking closure support. (Note I say "a Basic", I know some can and some can't.) No way is it a new-style functional language, and it never will be. (Modulo http://conal.net/blog/posts/the-c-language-is-purely-functional/ http://conal.net/blog/posts/the-c-language-is-purely-functio... , of course.)
- loup-vaillant 17y agoSide point: I wasn't talking about programming languages. I was talking about programming styles. Then by my definition (which I no longer consider universal), programming in a functional style drives you crazy unless you can use functions as first class entities.
- jacquesm 17y agoThere is an interesting macro in the glusterfs package that gives you call/cc, across machines no less. I think functional programming is anything that tries to avoid side-effects for as long as they can be postponed. There are languages that put an accent on that but you could program in a functional style in just about any language that supports function calls.
- Davertron 17y agoThis might be a bit off topic, but since we're talking about functional languages... I've done a very small amount of functional programming (I did the first 5 or so problems from project Euler in Haskell, for example, just to see what Haskell was like), and there's one specific thing I was wondering about. First, let me give you some example psuedo-code just to describe the situation: if function_call(value) == some_value_I_want_or_something ? return function_call(value); else return 0; Ok, that's not any particular language or anything, but I think it'll serve to get my query across. So in the above code, I want to do something like check the result of a function call, and then if that value is the one I want, return the result of that function call. Otherwise, return 0 (you could do whatever here, but I just made it simple...). My question is this: do most functional languages do some sort of optimization to avoid basically calling the function twice, since in a purely functional language, functions shouldn't have side affects, so calling a function multiple times with the same input should yield the same output? In other words, does the runtime/compiler/whatever say "I just ran function_call(value) and got a result, and since I don't allow side affects, anytime from here on out that I see function_call(value), I'm just going to assume the result is the same."? Maybe it's because I'm very inexperienced with functional languages, but I find that I run into that kind of situation often enough, and I always think "Is this going to be really slow because I'm basically calling this function twice even though the result is going to be the same?". I'm guessing that this may vary language to language; as I understand it, for example, Lisp is "functional", but not purely so, and doesn't really enforce the whole "no side-effects" thing, so it would have to run the function twice, but I could be mistaken.
- wlievens 17y agoYour pseudo-code is a little weird (it recurses infinitely?) but I think that what you're referring to is memoization, or basically caching your function calls' results for equivalent arguments. I don't know if any languages support it out the box, but I do know that any language with first-class functions and closures will allow you to make a simple "wrapper" construct so that you get this feature anyway.
- Davertron 17y ago