# What is a functional language

• (2 Pages)
• 1
• 2

## 26 Replies - 35949 Views - Last Post: 07 December 2015 - 01:15 AM

### #1 NotarySojac

• D.I.C Regular

Reputation: 53
• Posts: 428
• Joined: 30-September 10

# What is a functional language

Posted 31 May 2011 - 09:57 AM

For a long time I've been foggy on what functional languages are exactly. I poked around in f# briefly before giving up, very confused. Now I'm hearing that ruby is a functional language too. That would explain why its syntax often eludes me. Anyway, I wrote to myself a brief explanation of what a functional language is, but I'm missing some concrete examples of why functional languages are necessitated, and I'm not sure what I wrote is correct because when I read other descriptions of functional languages, they seem to contrast. Here's what I wrote to myself:

```# ruby can be used as a functional language!
# This is really cool for people who want to recall their math skills
# In a programming environment
#
# Functional means that...
# Instead of doing a function to find a value AT A POINT IN TIME
# You instead do a function to be used LATER ON to calculate a value
#
# For instance...

y = 10
iNeedThisValueNow = 13 + 5 + 2 + y        # => 30

SuperCoolFunction(iNeedThisValueNow)

# Ok, that's the usual kind of programming,
# but what if we want to do a function down the road many times?
#
# then you'd do

iNeedThisEQUATIONNow = lambda{|y| 13 + 5 + 2 + y}

iNeedThisValueNow = iNeedThisEQUATIONNow.call(10)       # => 30

# alternaitvely
iNeedThisValueNow = iNeedThisEQUATIONNow[10]

```

Is This A Good Question/Topic? 0

## Replies To: What is a functional language

### #2 MitkOK

• D.I.C Regular

Reputation: 32
• Posts: 403
• Joined: 09-August 07

## Re: What is a functional language

Posted 31 May 2011 - 12:23 PM

You are far away from understanding functional programming. Ruby has some features but it's an almost pure OOP lang.
GO find some book on lisp or haskell. Then try clojure or erlang.

### #3 Dogstopper

Reputation: 2972
• Posts: 11,223
• Joined: 15-July 08

## Re: What is a functional language

Posted 31 May 2011 - 12:26 PM

Even though you asked a question with code in Ruby, I'm going to move this topic to Functional Programming so that you can better understand what it is.

### #4 I X Code X 1

Reputation: 47
• Posts: 66
• Joined: 28-July 10

## Re: What is a functional language

Posted 31 May 2011 - 02:34 PM

You could argue that functional programming is a very broad topic. For something to be purely functional it needs to be written entirely in a functional programming way; 99% of Haskell does this, but even with that it is hard to write code in a completely functional way. Functional programming is a way to write code in which the functions "evaluate to a result".

Here's some bullet point for functional programing:

1. Functions always return the same result, as long as the same arguments are passed into it.

2. The function never references variables that are defined outside the function (unless they will without a doubt remain constant)

3. No variables are mutated/modified by the function

4. All you want to be returned is a result, no fancy printing or anything along those lines.

5. The function does not take outside input (keyboard, etc...)

Here's an example of a function written in functional style (notice that I have included a non-functional part to illustrate the difference:

```; functional style - all it does is return a result and mutates data
(cons widget database))

(defparameter *database* nil)

; non-functional - returns other parts that are more than just a result
(defun main-loop ()
(format t "The database contains the following: ~a~%" *database*)))
```

This program simply asks for an entry and stores it in a list.

Other things to know about functional programming:

1. High focus on higher-order programming: Lets you do code composition (combine chunks of code) to work on one main goal. This is functions that accept other functions as parameters.

2. Functional programming can reduce bugs - only returns one result that can be expected every time given a certain argument. So there should be no unexpected results which can happen in imperative programming.

3. Functional programs are more compact (usually) - much code is used in imperative programs to declare variables, updating, etc. This is avoided in functional programming.

4. Functional programming is more elegant - Brings us back to the domain of mathematics. Might be able to use tools of mathematics to help solve the problem.

Hope this helps! I am fairly new to it too, so I probably left out some other important details. But this is a general introduction to it!

This post has been edited by I X Code X 1: 31 May 2011 - 02:39 PM

### #5 Raynes

• D.I.C Lover

Reputation: 615
• Posts: 2,815
• Joined: 05-January 09

## Re: What is a functional language

Posted 11 June 2011 - 02:14 PM

I somehow managed to miss this, but I'll happily reply now.

Functional programming *is* a very broad topic. Where it begins and where it ends is not important.

You seem surprised that people consider Ruby to be a functional language. The definition of functional programming is that there isn't one. Everybody has a different idea in their head about what constitutes a functional programming language. Really, the only thing that isn't really arguable is that a functional language *must* have first-class functions. At the very core, a functional programming language really *needs* to have that. Ruby has that, so it'll be considered a functional language.

However, that doesn't mean you can learn it and then call yourself a functional programmer. Not at all. Ruby *supports* some level of functional programming, but that's about it. If you want to write code in a functional style in Ruby, you have to know how, and Ruby isn't going to help you do it. Ruby is not a suitable language for learning functional programming.

Languages like Haskell and Clojure support functional programming on a different level than Ruby. In both cases, the entire language is built around functional programming. In fact, Haskell is a purely functional programming language! Purity means a lot of things, but the core of that is that there is no side effects and in turn, no mutable state. Clojure isn't as strict as that and isn't pure, but it strongly encourages a functional style. You can't write good Clojure code without writing good functional code, and this is where Clojure departs from similarities with Scala, Ruby, and other languages that support functional programming but do not help enforce or encourage it.

Now, you also seem to be having trouble understanding what being a functional language means. I think a few examples will help you out a lot. I'll use Clojure for these examples, but I'll illustrate them thoroughly.

So, we have a problem. In a procedural language, how would one approach the task of doing something a number of times? For example, I want to square a number 10 times. How would we do that? In a procedural language, we might use some sort of for loop and a mutable variable.

```user=> (def number (atom 2))
#'user/number
user=> (loop [x 1] (when (< x 11) (do (swap! number #(* % %)) (recur (inc x)))))
nil
user=> @number
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

```

Here, we define a mutable variable called 'number'. It is bound, initially, to the number 2. Then, we do a very procedural-like loop. x begins as one, and we check to see if it is less than 11. If so, we mutate our number variable, squaring the number it contains, and then we increment x and restart the loop. This basically a what a for loop does, only we're actually using recursion here. We could make x an atom like number and it would be mutable as well, but the fact that number is mutable is enough nasty for one example.

Now, this works fine. But this is absolutely, positively, undeniably NOT have you should even *consider* approaching this problem in a functional programming language. There is so much wrong with this. Here is how we could solve it in idiomatic Clojure:

```user=> (reduce (fn [x _] (* x x)) 2 (range 1 11))
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

```

The above is nice, but this is even better:

```user=> (nth (iterate #(* % %) 2) 10)
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

```

Look how much shorter and cleaner the code is? What iterate does is simple: it takes a function and a starting value, and then it applies the function to the starting value, then it applies the function to the value produced by that, and then by that, and then by that, over and over again infinitely, building up a list of the results. Because Clojure has lazy sequences, it doesn't matter that this sequence is infinite, because we can just get what we need from it, in this case, the 10th value. This is what functional programming is all about. Immutability. We're working with sequences instead of mutable variables. We're using higher order functions (functions that take other functions as arguments) to compose smaller functions into powerful problem solvers. This, my friend, is functional programming.

### #6 UziTech

Reputation: 7
• Posts: 64
• Joined: 26-October 10

## Re: What is a functional language

Posted 16 June 2011 - 08:25 AM

What I don't understand about functional programming is why people still use it when anything you can do with a functional language you can do with an OO language, right?

### #7 xclite

• I wrote you an code

Reputation: 1413
• Posts: 4,263
• Joined: 12-May 09

## Re: What is a functional language

Posted 16 June 2011 - 08:29 AM

Anything you can do in an OO language, you can do in assembly. Why do we use OO languages?

### #8 UziTech

Reputation: 7
• Posts: 64
• Joined: 26-October 10

## Re: What is a functional language

Posted 16 June 2011 - 09:01 AM

xclite, on 16 June 2011 - 09:29 AM, said:

Anything you can do in an OO language, you can do in assembly. Why do we use OO languages?

Because they are easier to understand.

### #9 Raynes

• D.I.C Lover

Reputation: 615
• Posts: 2,815
• Joined: 05-January 09

## Re: What is a functional language

Posted 16 June 2011 - 06:17 PM

UziTech, on 16 June 2011 - 04:25 PM, said:

What I don't understand about functional programming is why people still use it when anything you can do with a functional language you can do with an OO language, right?

This is an inherently silly question that doesn't really deserve an answer, but I suppose I'll bite.

What do you mean by "still use it"? Functional programming has been around for a while in one form or another, but only recently has it begun to gain popularity and recognition. Functional programming isn't some old, outdated technology. To the contrary, it is in some ways the evolution of programming.

First of all, it isn't about what you can do. It is *never* about what you can do. Brainfuck is a turing complete programming language. Did you know that? That means that anything a more sensible language like Java or Ruby can compute, Brainfuck can compute as well. Why do you use Ruby?

You're correct for the most part, we use Ruby because it makes more sense than Brainfuck. It is more elegant, and we can express what we want easier and more clearly. This applies to functional programming as well. We use functional programming because functional programming allows us to express problems in cleaner, more concise, and easier ways.

You're asking the wrong question. It isn't "why do we use FP when we can write code that does the same thing in OOP". That is a nonsensical question. The right question is "Why do we use FP over OOP?", a question that I partially answered in my above reply to this topic.

In summary: Functional programming is a different way of thinking. We can express problems in different and generally better ways. You can write the same code in two different languages, but there are a million and one things that go into making one solution better than another, and that is why different paradigms and languages exist.

### #10 xclite

• I wrote you an code

Reputation: 1413
• Posts: 4,263
• Joined: 12-May 09

## Re: What is a functional language

Posted 16 June 2011 - 06:19 PM

UziTech, on 16 June 2011 - 12:01 PM, said:

xclite, on 16 June 2011 - 09:29 AM, said:

Anything you can do in an OO language, you can do in assembly. Why do we use OO languages?

Because they are easier to understand.

If you think about why it's easier to understand, and then think about how other paradigms might also improve the way we program, you'll have the answer to your question. Or you can read the essay by Raynes.

### #11 UziTech

Reputation: 7
• Posts: 64
• Joined: 26-October 10

## Re: What is a functional language

Posted 16 June 2011 - 07:07 PM

Raynes, on 16 June 2011 - 07:17 PM, said:

In summary: Functional programming is a different way of thinking. We can express problems in different and generally better ways. You can write the same code in two different languages, but there are a million and one things that go into making one solution better than another, and that is why different paradigms and languages exist.

What my question was asking was, in what ways is functional programming better?

### #12 Raynes

• D.I.C Lover

Reputation: 615
• Posts: 2,815
• Joined: 05-January 09

## Re: What is a functional language

Posted 16 June 2011 - 09:29 PM

No, really it wasn't. Either that, or it was very poorly worded.

Nonetheless, this is a question that I can be of assistance on.

One of the most important benefits of functional programming is that it makes concurrency easy, because concurrency is natural in a functional language. Concurrency is hard, and OOP language tend to make it harder because OOP is based around the manipulation of mutable state. When it comes down to mutating state over hundreds and thousands of threads, locking, synchronizing, and protecting data is insanely difficult. Functional programming eases the problem of mutable state with pure functions and the valuing of immutability over mutability.

Here is an example:

```user=> (def x (atom 0))
#'user/x
user=> (defn foo [] (swap! x inc))
#'user/foo
user=> @x
0
user=> (foo)
1
user=> @x
1
user=> (foo)
2
user=> @x
2

```

This is the sort of code that you might see in an object oriented language. A class might have some mutable field, such as a counter, that methods in that class update when called. In a functional language, the above might not seem like the greatest of ideas. Instead, we might opt to make our foo function pure:

```user=> (defn foo [x] (inc x))
#'user/foo
user=> (foo 10)
11
user=> (def x (foo 0))
#'user/x
user=> (def y (foo x))
#'user/y
user=> x
1
user=> y
2

```

Instead of modifying values in place, we're just returning the value modified and leaving the old value unchanged. Our new foo function is totally pure. It takes an argument, does something with the argument, and returns the result. There are no side effects, no talking with the outside world, nothing that could ever cause a race condition.

This example is small, but this simple change of design can mean the difference between an insanely complex system and a simple, safer, easier to maintain and reason about system.

Speaking of reasoning, another huge benefit of functional programming is how easy it is to reason about. There is a lot of complexity in our original foo function. It is modifying the value of a mutable variable outside of the function. There are a lot of variables (no pun intended) that go into that. But our foo function -- it's completely obvious what is going on. It's easy to reason about. One thing is happening and one thing alone: a number is being incremented. If you put in 1, you'll *always* get back 2, no exceptions.

In summary: Object orientation encourages and supports mutability by nature. Functional programming does the opposite. This means code easier to reason about, in a lot of cases more concise, and helps ease the pain of immutability which is important given our multicore future. This only scratches the surface. Why not give it a go? Even if you never end up using a functional programming language for anything significant, it can easily effect how you program in the languages you do use, usually for the better!

But, don't focus on what is 'better'. You can come to your own conclusions about what is better than what with the information you gain with experience.

### #13 UziTech

Reputation: 7
• Posts: 64
• Joined: 26-October 10

## Re: What is a functional language

Posted 17 June 2011 - 06:20 AM

Raynes, on 16 June 2011 - 10:29 PM, said:

No, really it wasn't. Either that, or it was very poorly worded.

Nonetheless, this is a question that I can be of assistance on.

One of the most important benefits of functional programming is that it makes concurrency easy, because concurrency is natural in a functional language. Concurrency is hard, and OOP language tend to make it harder because OOP is based around the manipulation of mutable state. When it comes down to mutating state over hundreds and thousands of threads, locking, synchronizing, and protecting data is insanely difficult. Functional programming eases the problem of mutable state with pure functions and the valuing of immutability over mutability.

Here is an example:

```user=> (def x (atom 0))
#'user/x
user=> (defn foo [] (swap! x inc))
#'user/foo
user=> @x
0
user=> (foo)
1
user=> @x
1
user=> (foo)
2
user=> @x
2

```

This is the sort of code that you might see in an object oriented language. A class might have some mutable field, such as a counter, that methods in that class update when called. In a functional language, the above might not seem like the greatest of ideas. Instead, we might opt to make our foo function pure:

```user=> (defn foo [x] (inc x))
#'user/foo
user=> (foo 10)
11
user=> (def x (foo 0))
#'user/x
user=> (def y (foo x))
#'user/y
user=> x
1
user=> y
2

```

Instead of modifying values in place, we're just returning the value modified and leaving the old value unchanged. Our new foo function is totally pure. It takes an argument, does something with the argument, and returns the result. There are no side effects, no talking with the outside world, nothing that could ever cause a race condition.

This example is small, but this simple change of design can mean the difference between an insanely complex system and a simple, safer, easier to maintain and reason about system.

Speaking of reasoning, another huge benefit of functional programming is how easy it is to reason about. There is a lot of complexity in our original foo function. It is modifying the value of a mutable variable outside of the function. There are a lot of variables (no pun intended) that go into that. But our foo function -- it's completely obvious what is going on. It's easy to reason about. One thing is happening and one thing alone: a number is being incremented. If you put in 1, you'll *always* get back 2, no exceptions.

In summary: Object orientation encourages and supports mutability by nature. Functional programming does the opposite. This means code easier to reason about, in a lot of cases more concise, and helps ease the pain of immutability which is important given our multicore future. This only scratches the surface. Why not give it a go? Even if you never end up using a functional programming language for anything significant, it can easily effect how you program in the languages you do use, usually for the better!

But, don't focus on what is 'better'. You can come to your own conclusions about what is better than what with the information you gain with experience.

"There is a lot of complexity in our original foo function."
Really, where is this complexity. I see you incrementing x that's it! Not very complex.

"If you put in 1, you'll *always* get back 2, no exceptions."
In BOTH functions if you put in 1 you will *always* get 2.

I think I am starting to come off as condescending. Believe me I don't mean to. I have been programing for about 10 years and have written programs in a couple functional languages but have never fully understood why anyone would use a functional language.

From what I understand you can write a "functional" program with an object oriented language but you cannot write an object oriented program with a functional language. If this is wrong please tell me.

What I am asking now is... Is there any reason one would want to use a functional programming language over an OO language?

### #14 I X Code X 1

Reputation: 47
• Posts: 66
• Joined: 28-July 10

## Re: What is a functional language

Posted 17 June 2011 - 06:37 AM

Quote

From what I understand you can write a "functional" program with an object oriented language but you cannot write an object oriented program with a functional language. If this is wrong please tell me.

Many functional languages have support for object-oriented programming. People tend to be under the misconception that object-oriented programming is "new". This is actually not the case at all. Lisp was created by John McCarthy around 1950 and at first it did not have object-oriented support; however, very soon after people started to implement object-oriented aspects into the language, soon it was even put into the ANSI Standard. This is the Standard that most common-day lisp's follow. Including Common Lisp. I suggest you take a look at the Common Lisp Object System (CLOS) to get a very good understanding of how Object-oriented programming is used in functional languages. However, you need to have an open mind to it, because you'll find that there are different ways to use object-oriented programming.

This link can give you a very brief guide to it and this book can and probably will change your perspective on object-oriented programming in functional languages.

Edit:
Also, I just want to add a reason why someone might choose to use a functional language, for example Common Lisp. Suppose someone is skilled in Common Lisp, and he writes all of his programs in this language. However, as he goes along he finds that he wishes Common Lisp had support for something that only another language, such as C or Java, has. Well, this is where the Common Lisp Macro System comes into play. The Macro System is used to make Domain Specific Lanagues (DSL). This means that one can use the system to implement parts of the language that is not already in the standard.

Of course not all lisp's have Common Lisp's Macro System but all are very versatile and can be used to one's will. It's up to the user to ultimately decide what language he prefers, but why not use functional languages when you can, in a sense, have all the languages at once?

Should be noted though, that Common Lisp is not purely functional. If one did not want to write in a functional style, they would not have to.

This post has been edited by I X Code X 1: 17 June 2011 - 07:13 AM

### #15 Raynes

• D.I.C Lover

Reputation: 615
• Posts: 2,815
• Joined: 05-January 09

## Re: What is a functional language

Posted 17 June 2011 - 12:32 PM

UziTech, on 17 June 2011 - 02:20 PM, said:

"There is a lot of complexity in our original foo function."
Really, where is this complexity. I see you incrementing x that's it! Not very complex.

Not in this particular function, no. You've got me there. However, let's create a new scenario: 300 different threads are also mutating this same variable. That is most definitely complex.

UziTech, on 17 June 2011 - 02:20 PM, said:

"If you put in 1, you'll *always* get back 2, no exceptions."
In BOTH functions if you put in 1 you will *always* get 2.

Nope. There is a side effect. The variable, x, is not encapsulated in the function. It is a mutable variable. If you pass 1 to foo the first time, you get 2. Pass it a second time, you get 3. At least, you *think* and *hope* that you do. What if, right before you passed 1 the second time, 3 other threads also incremented the variable? You'd get back 6. What if you weren't expecting that? You understand how bugs like this can be damn near impossible to debug in a large stateful application?

Now, I'll be the first to admit, my examples are simplistic, but apply them to the larger cases and you'll understand the complexity.

UziTech, on 17 June 2011 - 02:20 PM, said:

I think I am starting to come off as condescending. Believe me I don't mean to. I have been programing for about 10 years and have written programs in a couple functional languages but have never fully understood why anyone would use a functional language.

It's fine. That's what I'm here for. The only problem we're having is that I'm having trouble explaining it in a way that you'll understand. I 100% welcome the opportunity to try, in any case.

UziTech, on 17 June 2011 - 02:20 PM, said:

From what I understand you can write a "functional" program with an object oriented language but you cannot write an object oriented program with a functional language. If this is wrong please tell me.

It sounds like you're not really understanding the point of paradigms. A language is object oriented because it implements a set of concepts and provides a set of features that people have come to expect from object oriented languages. Polymorphism, encapsulation, etc. There is nothing stopping functional languages from implementing those concepts as well, or hell, implementing a full fledged object system (Scala does this, and so does OCaml and Common Lisp). Clojure has extremely flexible polymorphism via mulitmethods, and even some contract programming via protocols (similar to interfaces) and records (the implementing 'class', a type). Is it an object oriented language? Absolutely not, but it doesn't completely dismiss object orientation because not everything about object orientation is inherently bad. This is my bigger point in all of this: neither is 'better' in the traditional sense, because they both have good ideas. Good, modern languages do not strive to be functional languages. They strive to be better than their predecessors, and this means that they have to take what they deem good and necessary from their predecessors and implement it in a logical and reasonable way!

Building on the above, a lot of object orientated languages also implement some concepts from functional programming, namely immutability on some level and more importantly, first class functions. If you have at the very least first class functions, you can probably write code in a functional style in that language. C# and Ruby are excellent examples of this.

If you'd like, I could write up a few examples of object-orientedish code that I could write in Clojure as an example. I'm sure somebody would be willing to write up a small functional example in C# or Ruby as well if you'd like. But yes, you can write functionalish code in a lot of existing predominately OOP languages, and you can write object-orientedish code in a lot functional languages just the same.

UziTech, on 17 June 2011 - 02:20 PM, said:

What I am asking now is... Is there any reason one would want to use a functional programming language over an OO language?

Yes. You can write functionalish code in a lot of OOP languages, but that isn't a substitute for a real functional language. A language that tries to be several paradigms at once will always give a little in some areas. If you really want to learn functional programming and get the real payload out of it, you'd want to learn a serious functional language like Clojure. Languages like Clojure and Haskell are designed *around* functional programming, and didn't have functional programming tacked on as a passing thought. If you use a language like Ruby and then move to a language like Clojure, the point I'm trying to make becomes clear.

Also, despite me sounding a bit condescending earlier myself, I absolutely don't mind this discussion. I'm sure there are other people around with the same questions and feelings, and this topic will be a great resource for them, which is why I pinned it. If you have any other questions and concerns, go ahead and reply.