BeuysVonTelekraft Posted September 28, 2011 Posted September 28, 2011 I'm kinda new to programming. What does functional programming means? And what does imperative means? I've already searched for definitions but their implications weren't clear to me.
Schrödinger's hat Posted September 28, 2011 Posted September 28, 2011 (edited) I'm kinda new to programming. What does functional programming means? And what does imperative means? I've already searched for definitions but their implications weren't clear to me. In functional languages, everything is defined as a function ie. this is that. They tend to be well suited to recursion (defining something in terms of itself). You might have something like: factorial 0 = 1 factorial n = n * factorial (n - 1) Then call it with something like: ans = factorial 5 Functions are first class objects, they can be treated just like variables. Whereas in an imperative language you have to describe the process to get the result as a series of instructions, or operations ie. to get this, do that. You might have something like: void factorial(n, dest) { dest = 1; for (int i=1; i < n; i++) { dest *= i; } return; } Then call it with something like: factorial(5, ans); This is even more imperative in style than you would typically write it in a C-like language, but illustrates the distinction. Edited September 28, 2011 by Schrödinger's hat
khaled Posted September 28, 2011 Posted September 28, 2011 Functional Programming languages are basically Lambda Calculus, LISP is one example ... In functional programming, you don't have operators such as addition, subtraction ..etc, instead you have lambda operator from which you define your own operators, In general, functional programming is used for Artificial Intelligence, and Logic .. Another example is Prolog. On the other hand, we have Imperative programming languages, such as Python, C, C++, C#, Java, ..etc Usually programs that you may normally do in imperative programming, you would die to code them in functional.
Schrödinger's hat Posted September 28, 2011 Posted September 28, 2011 (edited) Functional Programming languages are basically Lambda Calculus, LISP is one example ... In functional programming, you don't have operators such as addition, subtraction ..etc, instead you have lambda operator from which you define your own operators, Well for starters, LISP comes with operators out of the box, they just work a little differently -- (+ 3 4 5) will evaluate as 12. This seems to me to be a bit of an extreme view, akin to saying imperative languages don't have functions (ie. the only imperative language by that definition would be assembly without macros). I was under the impression that functional to imperative was more of a spectrum (in the style of the use of the language as well as the language itself) than a hard line: At one end we have lisp, prolog, or pure lambda calculus. Then maybe Haskell and a few others. I don't really have any experience with these so I don't know quite where to put them There's a large middle ground, I'd put pyhton, javascript and so on here, as they are amenable to both functional and imperative styles. Then we'd have C, fortran, Java etc. And at the extreme imperative end, assembly. This is probably a bit simplistic as stack/array based languages don't really fit on this scale anywhere. Edited September 28, 2011 by Schrödinger's hat
khaled Posted September 28, 2011 Posted September 28, 2011 Lambda Calculus is based on three rules, as a primitive programming language, since programming languages in mathematical logic are nothing but a State Machines, where you have a graph of the language symbols, such early works by mathematical logic scientists such as Turing, Golden, Post, ..etc
Greg Boyles Posted September 28, 2011 Posted September 28, 2011 I'm kinda new to programming. What does functional programming means? And what does imperative means? I've already searched for definitions but their implications weren't clear to me. I'm kinda new to programming. What does functional programming means? And what does imperative means? I've already searched for definitions but their implications weren't clear to me. When I did my graduate diploma in computer science we covered a bit of LISP. I personally found it very unintuitive and difficult to debug due to the necessity for umpteen recursive calls resulting in a horrible mess of nested brackets. I find it far easier to program on a one step per line basis as in imperative languages.
Schrödinger's hat Posted September 29, 2011 Posted September 29, 2011 When I did my graduate diploma in computer science we covered a bit of LISP. I personally found it very unintuitive and difficult to debug due to the necessity for umpteen recursive calls resulting in a horrible mess of nested brackets. I find it far easier to program on a one step per line basis as in imperative languages. From what I read, both styles have their uses, and you don't have to go all the way to LISP to take advantage of functional style. Here's someone saying it in more elegant words than I can: http://www.joelonsoftware.com/items/2006/08/01.html 1
BeuysVonTelekraft Posted October 1, 2011 Author Posted October 1, 2011 factorial 0 = 1 factorial n = n * factorial (n - 1) Then call it with something like: ans = factorial 5 I didn't get how this works. factorial 0 = 1 factorial n = n * factorial (n - 1) You're making a new function "factorial". And n factorial will be n * factorial(n-1). But how can it be possible if it still does not know what is factorial? This sounds really weird to me. You just gave the factorial 0=1, how can it guess the result for other n's? Or factorial is a built-in function?
Schrödinger's hat Posted October 1, 2011 Posted October 1, 2011 (edited) I didn't get how this works. factorial 0 = 1 factorial n = n * factorial (n - 1) You're making a new function "factorial". And n factorial will be n * factorial(n-1). But how can it be possible if it still does not know what is factorial? This sounds really weird to me. You just gave the factorial 0=1, how can it guess the result for other n's? Or factorial is a built-in function? Well it's what's called a recursive function definition. You can do them in some/most imperative languages too, but they don't always handle them as well. What the haskell vm will do when it sees you ask for, say, factorial 4 is very loosely: factorial 4 -- I don't know what this is, but i have a definition that says factorial 4 is: 4*factorial 3 -- well factorial 3 is 3*factorial 2 so: 4*(3*factorial 2) 4*(3*(2*factorial 1)) 4*(3*(2*(1*factorial 0))) -- factorial 0 is defined as a number so 4*(3*(2*(1*(1)))) 24 This is a very functional-styled way of thinking about the problem of finding a factorial. I also don't quite know what compiling this (haskell does have a compiler) entails, Probably converting it to an equivalent, but more imperative, algorithm. Edited October 1, 2011 by Schrödinger's hat 1
BeuysVonTelekraft Posted October 1, 2011 Author Posted October 1, 2011 Well it's what's called a recursive function definition. You can do them in some/most imperative languages too, but they don't always handle them as well. What the haskell vm will do when it sees you ask for, say, factorial 4 is very loosely: factorial 4 -- I don't know what this is, but i have a definition that says factorial 4 is: 4*factorial 3 -- well factorial 3 is 3*factorial 2 so: 4*(3*factorial 2) 4*(3*(2*factorial 1)) 4*(3*(2*(1*factorial 0))) -- factorial 0 is defined as a number so 4*(3*(2*(1*(1)))) 24 This is a very functional-styled way of thinking about the problem of finding a factorial. I also don't quite know what compiling this (haskell does have a compiler) entails, Probably converting it to an equivalent, but more imperative, algorithm. I've read about it on "An introduction to Programmming with Mathematica". There, they have a similar function that sounds plausible to me. Using recursion to discover what are the next fibonacci numbers. But this: factorial 0 = 1 factorial n = n * factorial (n - 1) Is completelly alien to me, on the same book, i've seen a similar example of your demonstration of recursion.
Schrödinger's hat Posted October 1, 2011 Posted October 1, 2011 (edited) I've read about it on "An introduction to Programmming with Mathematica". There, they have a similar function that sounds plausible to me. Using recursion to discover what are the next fibonacci numbers. But this: factorial 0 = 1 factorial n = n * factorial (n - 1) That code is Haskell (i'm not that familiar with Haskell, it was just an example I'd seen recently that came to mind). In the mathematica syntax of your other example (bearing in mind that my total experience with mathematica is 5 minutes, so this may not be right) it would be: In[1]:= F[0] = 1; F[n_] := n * F[n] /; n>0 But bear in mind that recursion isn't what makes a functional language, just one of the things that is a functional style of thing to do. If you were more wondering how the Haskell code works. The way I like to think of it is that the language designers are wizards. On a more serious note, there'll be some way the VM/compiler assigns priority to the definition for factorial 0 =1 (whether it's that it was defined with a constant, or where it appears in the program, I'm not sure), but if it doesn't see the 0 it takes the general case of the factorial n definition. Edited October 1, 2011 by Schrödinger's hat
BeuysVonTelekraft Posted October 1, 2011 Author Posted October 1, 2011 That code is Haskell (i'm not that familiar with Haskell, it was just an example I'd seen recently that came to mind). In the mathematica syntax of your other example (bearing in mind that my total experience with mathematica is 5 minutes, so this may not be right) it would be: In[1]:= F[0] = 1; F[n_] := n * F[n] /; n>0 But bear in mind that recursion isn't what makes a functional language, just one of the things that is a functional style of thing to do. If you were more wondering how the Haskell code works. The way I like to think of it is that the language designers are wizards. On a more serious note, there'll be some way the VM/compiler assigns priority to the definition for factorial 0 =1 (whether it's that it was defined with a constant, or where it appears in the program, I'm not sure), but if it doesn't see the 0 it takes the general case of the factorial n definition. It happens a nasty error when n is > 2.
Greg Boyles Posted October 1, 2011 Posted October 1, 2011 (edited) I didn't get how this works. factorial 0 = 1 factorial n = n * factorial (n - 1) You're making a new function "factorial". And n factorial will be n * factorial(n-1). But how can it be possible if it still does not know what is factorial? This sounds really weird to me. You just gave the factorial 0=1, how can it guess the result for other n's? Or factorial is a built-in function? Perhaps it would make more sense if I wrote the equivalent function in C int factorial(int n, int total = 1) { if (n == 0) return total; else return factorial(n-1, total*n) } Call it as x = factorial(10); Edited October 1, 2011 by Greg Boyles
Schrödinger's hat Posted October 1, 2011 Posted October 1, 2011 It happens a nasty error when n is > 2. Hahah, missed a bit, my bad. Try this: In[1]:= F[0] = 1; F[n_] := n * F[n-1] /; n>0
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now