19 ms·
I have a question for the hackers here. Im a beginner programmer (<1 year of real world programming experience so far). What is the kind of problems (web app in
by juliend2 18y ago
I have a question for the hackers here. Im a beginner programmer (<1 year of real world programming experience so far). What is the kind of problems (web app in particular) that functional programming helps to resolve? And if i dont use myself all the features of OOP in Ruby, does it makes me a "functional programmer"? There must be some concepts that are unique to functional programming. What is the Essence of functional programming vs OOP programming? Why functional programming? (it would be nice for me to have a wikipedia page about this but in simple english).
Thanks in advance.
- michaelneale 18y agoI think web apps are an interesting thing to try at least with FP - in a sense its functional: response = f(request). You certainly can do FP style in most languages, but when you at least play with something that is more a pure FP (haskell is great and beautiful) it really makes sense when you see those concepts applied elsewhere - an interesting way to use haskell is Happs: http://dbpatterson.com/articles/5 http://dbpatterson.com/articles/5 (the haskell app server). Just make sure its fun !
- arebop 18y agoI agree that functional programming and web applications fit nicely together. response = f(request) probably won't mean much to someone who has no familiarity with fp, though. http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_1_3 http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch... is a key section of a very readable explanation of web architecture. I think it could serve as a very practical, if unconventional, demonstration of the usefulness of referential transparency.
- michaelneale 18y agoyes I guess I made the assumption of some level of maths - but perhaps its not common for people starting out now (I know it took me years before I appreciated all the mathematical stuff).
- jrockway 18y ago> And if i dont use myself all the features of OOP in Ruby, does it makes me a "functional programmer"? No, that would be imperative procedural programming. (You are doing functional programming if your program flow can be described as a single sequence of function applications. This is hard in Ruby.) The rest of your questions are well-answered throughout the Internet.
- keenerd 18y agohttp://www.defmacro.org/ramblings/fp.html http://www.defmacro.org/ramblings/fp.html Uses a Java-esque language for code samples, and required reading for my new hires. This and others is part of a collection of introductory-but-mind-expanding articles.
- gcv 18y agoWith regard to web apps, you ask a big question, which I, for one, am unprepared to address in a brief comment. In general, however, functional programming aims to make it easier to think about what a program does as it runs. In imperative programming, you create variables and you change their values (or the values they reference). This is called mutable state. Functional programming claims that mutable state makes it difficult to reason about the value of a variable, because anything and anyone might have changed it. I happen to agree with this claim. Functional languages discourage, or outright forbid, changing variable values or referenced values. Instead, functional programs call functions in which the desired new value of the variable is an argument. Initially, for people weaned on imperative languages, this is a mind-bender. It turns out, however, that with a little practice (I'd say less than two days of reading through SICP), that functional programs can be shorter and easier to understand than imperative programs. It turns out that modifying state becomes even trickier to think about once you introduce multiple threads of control. Variables clobbered by different threads leads to subtle and difficult bugs. A purely functional approach aims to eliminate this source of bugs at its root.
- rkts 18y agoKool-Aid-free answer: "Functional programming" can mean one of three things. 1. Higher-order programming: the use of higher-order functions to make code more general/compact. Since you know Ruby, you're already doing some higher-order programming, but you may not understand it well enough to take full advantage of it. The best way to learn higher-order programming is to read SICP. 2. The use of immutable (aka persistent, nondestructive) data structures, i.e. data structures that can be updated without mutation. For example, linked lists allow you to prepend an element without changing the original list. This makes code cleaner in some cases and has advantages for concurrency. For specifics, look up Clojure. 3. Functional purity, aka referential transparency: the property that, for any input x, a function must always produce the same output x'. That is, all functions must be functions in the mathematical sense of the word. In practice, this doesn't really mean eliminating impure functions, but rather partitioning your program into "provably pure functions" and "possibly impure functions," using a clever hack (monads) to ensure that the output of possibly-impure functions is invisible to the provably-pure ones. So, supposing you had a pure function whose behavior you wanted to randomize, you couldn't just throw in a call to the random() function, but would also have to convert it and all the functions that depend on it into impure (monadic) functions, replacing all function calls with the bind operator (>>=). It's quite complicated and I have never found a reason to believe it's a good idea, other than some theoretical stuff about "safety" and compiler optimizations that make your program 2% faster. I suggest not wasting your time on it.
- blasdel 18y agoYou don't understand monads very well, they are orthogonal to 'impurity', and don't even necessarily involve state or sequencing at all. A more straightforward way to describe how you do random() is that your function takes the randomness as an argument. Functions that take input take IO as an argument, and those that produce output return IO in some form. You don't even have to mention the dreaded word.
- rkts 18y agomonads... don't even necessarily involve state or sequencing at all I never said that they do.
- vorador 18y agoYou should read Structure and Interpretation of Computer programs. It is available in french at Dunod iirc.
- olavk 18y agoBasically funtional programming helps resolve the problem of bugs. If you rarely make bugs in your code, you probably wont get any advantage from functional languages. They don't allow you to do anything practical which is not possible in OO or imperative languages (and vice versa). So, how does functional programming reduce bugs? The theory is that we make bugs because programs are too complex to grasp fully (even if we wrote the whole progam ourselves), which makes us make wrong assumptions about what happens in a given piece of code, and how that piece of code will influence other parts of the program. Functional programming tries to reduce the amount of different "invisible" outside factors which may determine what will happen in a given piece of code. This makes it easier to understand code and avoid mistakes.