Re: What is a functional language

Posted 17 June 2011 - 02:37 PM

No. He is saying that you should use functional languages when you want the full arsenal of functional tricks. There are many cases when the functional paradigm can attack the same problem in a different way. Instead of OOP where you tell the program HOW to compute something, functional focuses on what the problem is and WHAT you need to solve.

Re: What is a functional language

Posted 17 June 2011 - 02:59 PM

Honestly dude, it comes across as if you aren't reading Raynes's essays, so if you are, then I apologize. But seriously, if you want to learn, then stop asking the same question over and over and read the response given to you. Believe me there...

Re: What is a functional language

Posted 22 June 2011 - 01:12 AM

I'm not sure how my clearer my posts could have been.

It isn't about what a functional language can do that an object orientation language can not. Truckers can drive the big rigs and I can only drive cars and small trucks, but that doesn't make them a better human. I've said a million and one times in this thread to *stop* worrying about what is 'better'. That's missing the point.

The functional tricks are that a functional language typically offers everything you'd expect from a functional language. They're designed to be function languages, and thus it wasn't tacked on as an afterthought as it usually is in OOP languages (see C#). The word 'trick' was used poorly here, because they aren't tricks. They're simply concepts implemented. Higher order functions, closures, etc. Instead of "some things are sometimes immutable", it becomes "most things are always immutable". There is a massive difference, but you wont really get it until you've experienced it.

I've been presenting you with examples. I'm not really capable of describing every single good thing about functional programming right here in a few posts. It's hard enough to do it as a chapter of an entire book. These are simply examples of a few things that are good about FP, not intended to be all-inclusive.

So, concurrency and immutability rarely needs to be used? Sure, in an OOP language, you'd probably rarely find yourself using immutability. That's the point. In a functional language, you'd have and use immutability, because it makes code safer, easier to reason about, and frequently easier to write and understand. I've made this point already, but it seems to go unheard. I don't know what planet you're from where concurrency isn't "commonly needed to be used" though, so I can't help you there.

Alas, we're back to "what does FP do that OOP can't" and I've answered this question in this thread several times. There isn't anything you can do in an FP language that you can't do in an OOP language. Turing completeness and all that. But, there are plenty of things you can do in an FP language that ends up being cleaner, clearer, more concise, and generally better than the same thing in an OOP language. Your next question will be "what?" and my answer will be "go read some functional code and compare it to equivalent OOP code" and if you don't get it then, maybe you just never will.

Google is a huge resource of information, as you apparently know. defmacro.org's articles are great, so good catch. I encourage future readers of this thread to check that link out. 9 times out of 10, his articles explain things better than me. Wikipedia is a fantastic resource as well. All in all, you can ask people "what is so great about functional progamming?" until the end of time, but you're not going to understand it until you've worked with a functional language and have experienced it for yourself.

To UziTech directly: I apologize if my posts weren't clear or thorough enough for you. I certainly hope defmacro.org's article helped you out better than I did. Some people have more trouble 'getting it' than others.

Re: What is a functional language

Posted 27 June 2011 - 03:25 PM

Hey, I'm still a tiny bit lost, although I can now see that functional languages prevent race condition scenario issues.

I recently participated in a challenge to solve a soduko puzzle. This entailed using three 2D arrays: Rows, Columns, and Regions. (because sudoku boards have 9 rows, 9 columns, and 9 regions made up of the same 81 cells on the board).

The indexes of these arrays are where the math became a complication. For example, Row[0][0] is the first row array'sfirst slot. This implied that:

I also wrote a function to return which Region is mapped to Row x and Column y, given simply x and y.

def get_region(a, B)/>
g_region = lambda { |x, y| (y/3+1)*3 - (3-x/3) } # this mathematical function returns the region's INDEX to be used with SQ[bla]
g_region.call(a, B)/> # supply the function with the INDEX of the row and column you would like to find the region of
# I think there's a way to get it to solve backwards if I add the slot number to it too...
end

Alternatively, I could have written it as:

def get_region(x, y)
g_region = (y/3+1)*3 - (3-x/3)
end

Do any benefits of functional programming apply to these kinds of situations? In one of my comments, I wondered if I could somehow use that same lambda function to solver for s, where s stood for the slot in the region (and wasn't expressed anywhere since I was just learning the syntax of it all at the time).

Are there any speed advantages/disadvantages to using lamda assuming that it's a single(multiple) thread(s) executing the code?

Re: What is a functional language

Posted 31 January 2013 - 10:44 AM

Let me see if I understand this portion of functional programming. The given code will computer forver on a list of values, and once you start it, it should never stop. If it hasn't computed the value you need as of yet, you might have to wait for a while, few minutes to a few million years? Does this mean, then, that it would work best on say, a mainframe, where the mainframe is constantly working on the solution, and the users are polling for the desired result? Maybe I'm misunderstanding.

Re: What is a functional language

Posted 02 February 2013 - 05:47 AM

Pwn, on 31 January 2013 - 06:44 PM, said:

Let me see if I understand this portion of functional programming. The given code will computer forver on a list of values, and once you start it, it should never stop. If it hasn't computed the value you need as of yet, you might have to wait for a while, few minutes to a few million years?

When you say "the given code", are you referring to a specific piece of code posted or described in this thread? If so, which one? Or do you just mean "code generally written in a functional programming language"?

Either way what you describe doesn't really have anything to do with functional programming. It's sort of similar to how lazy evaluation works, but with lazy evaluation, values are calculated the first time you need them, not continuously. And of course lazy evaluation isn't a defining characteristic of functional programming languages (most functional programming languages are not lazily evaluated).

Re: What is a functional language

That's it. You have functional languages that are impure, others that are pure. Some are referentially transparent, others aren't. Some are not typed, others are statically typed. This does not matter when getting at the core of "Functional Programming".

Basically, what your language needs is this:

Function abstraction: Your language needs to be able to define functions by abstracting from values. If you have "5 + 10", you should be able to abstract it to "\x -> x + 10", as you should be able to abstract it to "\x -> \y -> x + y".
This also works with ANYTHING in your language, specially functions. If you have "max(5,10)", not only should you be able to abstract it to "\x -> \y -> max(x,y)", but you also need to be able to abstract it to "\f -> \x -> \y -> f(x,y)"

Function aplication: Because you abstract over functions and values, you need to be able to apply those functions over values. With the example above, since we were able to abstract "max(5,10)" to "\f -> \x -> \y -> f(x,y)", we should be able to apply that function over "max", "5" and "10" to get the same result. It works like this: "(\f -> \x -> \y -> f(x,y)) max 5 10" = "max(5,10)"

First-order functions: This is inherent in the 2 above. Your language needs to be able to use functions just as you would use normal values. This means you can abstract over them, and use them as arguments or results of function application. This is one of the core principles of the lambda calculus

After your language has these core features though, you can get interesting with a lot of other stuff. For instance, there are lots of types of lambda calculus. You have the untyped lambda calculus, but also the simple-typed one, System F, and other ones. Functional languages can be based on any of the above.
Not only that, they can do some tweaks here and there and change some stuff. For instance, your language may be based on the untyped lambda calculus, but have some primitive functions that allow you to call the database or the console. Others may include some imperative constructs, like variable assignment, sequence of instructions, etc, inside the scope of some functions, etc. Others may not allow any of the above and instead represent pure functions. Others may include more data structures and constructions, and ways to work with these (like algebraic data types coupled with pattern matching). But the more of this stuff the language has, the more blurry the line becomes on whether it's based on the lambda calculus, or if it just uses some things from it.