Short answer:
One of the elements which make people say that functional programming code is difficult to read is that it gives preference to a more compact syntax.
Long answer:
Functional programming itself cannot be readable or unreadable, since it's a paradigm, not a style of writing code. In C# for example, functional programming looks like:
return this.Data.Products
.Where(c => c.IsEnabled)
.GroupBy(c => c.Category)
.Select(c => new PricesPerCategory(category: c.Key, minimum: c.Min(d => d.Price), maximum: c.Max(d => d.Price)));
and would be considered readable by any person with enough experience in Java, C# or similar languages.
The language syntax, on the other hand, is more compact for many functional languages (including Haskell and F#) compared to popular OOP languages, giving a preference to symbols rather than word in plain English.
This applies to languages outside FP as well. If you compare popular OOP languages to some less popular which tend to use more English words, the last ones would feel easier to understand for people with no programming experience.
Compare:
public void IsWithinRanges<T>(T number, param Range<T>[] ranges) where T : INumeric
{
foreach (var range in ranges)
{
if (number >= range.Left && number <= range.Right)
{
return true;
}
}
return false;
}
to:
public function void IsWithinRanges
with parameters T number, param array of (Range of T) ranges
using generic type T
given that T implements INumeric
{
for each (var range in ranges)
{
if (number is from range.Left to range.Right)
{
return true;
}
}
return false;
}
In the same way:
var a = ((b - c) in 1..n) ? d : 0;
could be expressed in an imaginary language as:
define variable a being equal to d if (b minus c) is between 1 and n or 0 otherwise;
When shorter syntax is better?
While more verbose syntax is easier to understand for a beginner, more compact syntax is easier for experienced developers. Shorter code means less characters to type, which means more productivity.
Especially, it really doesn't make sense forcing a person to type a keyword to indicate something which may be deduced from the syntax.
Examples:
In PHP you need to type function
before every function or method, with no specific reason to do it.
Ada always horrified me for forcing the developers to type lots of English words, especially since there is no correct IDE with auto-completion. I haven't used Ada recently and their official tutorial is down so I can't give an example; if anybody has an example, feel free to modify my answer.
The syntax 1..n
, used in many FP (and Matlab), could be replaced by between 1, n
or between 1 and n
or between (1, n)
. The between
keyword makes it much easier to understand for people who are not familiar with the language syntax, but still, two dots are much faster to type.