views:

1887

answers:

12

Wikipedia says so, but I'm not convinced.Why or why not?

+8  A: 

Ruby is a multi-paradigm language that supports a functional style of programming.

skymt
+1  A: 

Ruby isn't really much of a multi-paradigm language either, I think. Multi-paradigm tends to be used by people wanting to label their favorite language as something which is useful in many different areas.

I'd describe Ruby is an object-oriented scripting language. Yes, functions are first-class objects (sort of), but that doesn't really make it a functional language. IMO, I might add.

JesperE
The type of language is defined by the programming styles it supports; that, in turn, is decided by the features it has. First-class and anonymous functions = minimal functional programming. Ruby supports OO programming but doesn't require it: you never need to define a class. Hence, multi-paradigm.
skymt
+6  A: 

I most definitely think you can use functional style in Ruby.

One of the most critical aspects to be able to program in a functional style is if the language supports higher order functions... which Ruby does.

That said, it's easy to program in Ruby in a non-functional style as well. Another key aspect of functional style is to not have state, and have real mathematical functions that always return the same value for a given set of inputs. This can be done in Ruby, but it is not enforced in the language like something more strictly functional like Haskell.

So, yeah, it supports functional style, but it also will let you program in a non-functional style as well.

Mike Stone
Using this criteria, would you say Smalltalk is functional because it has blocks?
OscarRyz
+2  A: 

Ruby is an object-oriented language, that can support other paradigms (functional, imperative, etc). However, since everything in Ruby is an object, it's primarily an OO language.

example:

"hello".reverse() = "olleh", every string is a string object instance and so on and so forth.

Read up here or here

camflan
+8  A: 

I submit that supporting, or having the ability to program in a language in a functional style does not a functional language make.

I can even write Java code in a functional style if I want to hurt my collegues, and myself a few months weeks on.

Having a functional language is not only about what you can do, such as higher-order functions, first-class functions and currying. It is also about what you cannot do, like side-effects in pure functions.

This is important because it is a big part of the reason why functional programs are, or functional code in generel is, easier to reason about. And when code is easier to reason about, bugs become shallower and float to the conceptual surface where they can be fixed, which in turn gives less buggy code.

Ruby is object-oriented at its core, so even though it has reasonably good support for a functional style, it is not itself a functional language.

That's my non-scientific opinion anyway.

Edit: In retrospect and with consideration for the fine comments I have recieved to this answer thus far, I think the object-oriented versus functional comparison is one of apples and oranges.

The real differentiator is that of being imparative in execution, or not. Functional languages have the expression as their primary linguistic construct and the order of execution is often undefined or defined as being lazy. Strict execution is possible but only used when needed. In an imparative language, strict execution is the default and while lazy execution is possible, it is often kludgy to do and can have unpredictable results in many edge cases.

Now, that's my non-scientific opinion.

Christian Vest Hansen
I think you can make a much better case for calling Ruby functional than Java.... no, Ruby is not strictly functional, but it is pretty easy to use a functional style in it... and a non-functional style colleague can easily change it back to non-functional
Mike Stone
Yes, Mike, if you want to code in a functional style, then Ruby is a huge improvement over Java. I was only using Java to exaggerate and hammer the point home.
Christian Vest Hansen
So since D has pure functions, you'd call D a functional language? http://www.digitalmars.com/d/2.0/function.html#pure-functions
Peter Burns
Many people consider Lisp and Scheme functional languages, largely due to the pervasive use of anonymous functions. And yet, they lack guaranteed pure functions. Limiting the term to languages that support pure functions seems far too restrictive.
skymt
Good points. I have revised my answer in accord.
Christian Vest Hansen
+15  A: 

Ruby does support higher-level functions (see Array#map, inject, & select), but it is still an imperative, Object-Oriented language.

One of the key characteristics of a functional language it that it avoids mutable state. Functional languages do not have the concept of a variable as you would have in Ruby, C, Java, or any other imperative language.

Another key characteristic of a functional language is that it focuses on defining a program in terms of "what", rather than "how". When programming in an OO language, we write classes & methods to hide the implementation (the "how") from the "what" (the class/method name), but in the end these methods are still written using a sequence of statements. In a functional language, you do not specify a sequence of execution, even at the lowest level.

Mike
+1  A: 

It depends on your definition of a “functional language”. Personally, I think the term is itself quite problematic when used as an absolute. The are more aspects to being a “functional language” than mere language features and most depend on where you're looking from. For instance, the culture surrounding the language is quite important in this regard. Does it encourage a functional style? What about the available libraries? Do they encourage you to use them in a functional way?

Most people would call Scheme a functional language, for example. But what about Common Lisp? Apart from the multiple-/single-namespace issue and guaranteed tail-call elimination (which some CL implementations support as well, depending on the compiler settings), there isn't much that makes Scheme as a language more suited to functional programming than Common Lisp, and still, most Lispers wouldn't call CL a functional language. Why? Because the culture surrounding it heavily depends on CL's imperative features (like the LOOP macro, for example, which most Schemers would probably frown upon).

On the other hand, a C programmer may well consider CL a functional language. Most code written in any Lisp dialect is certainly much more functional in style than your usual block of C code, after all. Likewise, Scheme is very much an imperative language as compared to Haskell. Therefore, I don't think there can ever be a definite yes/no answer. Whether to call a language functional or not heavily depends on your viewpoint.

Matthias Benkard
A: 

Please, have a look at the beginning of the book: "A-Great-Ruby-eBook". It discusses the very specific topic you are asking. You can do different types of programming in Ruby. If you want to program like functionally, you can do it. If you want to program like imperatively, you can do it. It is a definition question how functional Ruby in the end is. Please, see the reply by the user camflan.

Masi
A: 

Recursion is common in functional programming. Almost any language does support recursion, but recursive algorithms are often ineffective if there is no tail call optimization (TCO).

Functional programming languages are capable of optimizing tail recursion and can execute such code in constant space. Some Ruby implementations do optimize tail recursion, the other don't, but in general Ruby implementations are not required to do TCO. See Does Ruby perform Tail Call Optimization?

So, if you write some Ruby functional style and rely on TCO of some particular implementation, your code may be very ineffective in another Ruby interpreter. I think this is why Ruby is not a functional language (neither is Python).

jetxee
+9  A: 

Whether a language is or is not a functional language is unimportant. Functional Programming is a thesis, best explained by Philip Wadler (The Essence of Functional Programming) and John Hughes (Why Functional Programming Matters).

A meaningful question is, 'How amenable is Ruby to achieving the thesis of functional programming?' The answer is 'very poorly'.

I gave a talk on this just recently. Here are the slides.

Tony Morris
The slides you gave didn't mention _why_ Ruby is "very poorly amenable to achieving the thesis of FP." Why is C# more amenable than Java (OK, easier anonymous functions?)? Is it because you can have global variables in Ruby?
kizzx2
No the slides don't go into this detail, since this is quite an extensive topic. At the risk of excessive simplification, for example, Ruby enforces an evaluation model (call-by-value) that ensures non-compositionality of programs. The implications of this can easily be under-estimated. Ruby is also married to the idea that a program is a sequence of effects. That is to say, Ruby goes out of its way to make it difficult/intractible to use any other computational model. I hope this short comment helps.
Tony Morris
+1 for pointing out the ambiguity in classifying languages as functional. Hell, I've written functional C!
itsmyown
A: 

Strictly speaking, it doesn't make sense to describe a language as "functional"; most languages are capable of functional programming. Even C++ is.

Functional style is more or less a subset of imperative language features, supported with syntactic sugar and some compiler optimizations like immutability and tail-recursion flattening,

The latter arguably is a minor implementation-specific technicality and has nothing to do with the actual language. The x64 C# 4.0 compiler does tail-recursion optimization, whereas the x86 one doesn't for whatever stupid reason.

Syntactic sugar can usually be worked around to some extent or another, especially if the language has a programmable precompiler (i.e. C's #define).

It might be slightly more meaningful to ask, "does language __ support imperative programming?", and the answer, for instance with Lisp, is "no".

Rei Miyasaka