June 20, 2018
Hot Topics:

Scala vs. F#: Comparing Functional Programming Features

  • May 19, 2010
  • By Edmon Begoli
  • Send Email »
  • More Articles »

F# vs. Scala: List Comprehensions

Originally popularized by Haskell (another, archetypical functional language), list comprehensions are in mathematical terms set-builder, notation-based expressions for the definition of lists. For example, in Haskell one would use the following list comprehension to produce a list of nums containing only squares of numbers greater than 2:

squares = [ x*x | x <- nums, x > 2 ]

The F# equivalent for list comprehension is called a generator comprehension, and it can be used for both lists and sequences. In functional languages, sequences are similar to lists except that elements in the sequences (e.g., 1…1000) are calculated only when requested, making them more storage efficient.

List generators have the following form:

[for x in collection do ... yield expr]

Sequence (seq) generators have this form:

seq {for x in collection do ... yield expr}

So, the F# version of the previously shown Haskell list-generation example for multiples of values over 2 has the following syntax:

del list = [for x in 0..100 if x > 2 do yield x*x]

In Scala, list comprehension has the following structure:

val type = for ( range ) [if (condition)] ) yield result

Here is a Scala version for multiple list comprehension:

val list = for (i <- 1 to 100; if (i > 2)) yield i*i

F# vs. Scala: Multiple Inheritance via Mixins

One notable difference between F# and Scala is the lack of support for multiple inheritance in F#. In Scala, programmers can declare that a child extends from one primary class, but that it can also inherit "traits" from other classes.

In so-called mixin class composition, subclasses inherit through mixin the delta of a class definition (i.e., a class will inherit all new definitions that are not inherited through direct inheritance). In the example below, OtherParentClass is used as a mixin.

Class MixinSubclass extends BaseClass with OtherParentClass

Base classes are declared with the keyword extends and mixins (zero or many) are declared with keyword with.

Inherited methods are explicitly overridden with the keyword override to prevent accidental overriding:

override def calculate(dx: Int, dy: Int): Distance =
    new Distance(x + dy, y + dy )

OtherParentClass would be declared with keyword trait:

trait OtherParentClass
      def count:Int
      def kmDistance = count * 1.66


For the most part -- except for Scala mixins -- F# and Scala offer similar expressive power when it comes to functional programming. They both are flexible, powerful programming languages with strong functional features. In this article, I presented an abbreviated comparative review of the most essential functional programming features of these two languages.

Why abbreviated? Because functional programming is perhaps the most flexible type of programming, offering a number of small features and subtleties that would make a detailed comparison of all features very lengthy -- far beyond the scope of a single article.

In the next installment, I will compare and contrast the features where these two languages most differ: application development models and runtime characteristics.

For Further Reading

  • The Scala Programming Language homepage
  • The Microsoft F# Developer Center
  • About the Author

    Edmon Begoli is a software architect with 14 years of professional experience on large commercial and public software projects. He is a member of the R&D staff at Oak Ridge National Laboratory.

    Page 2 of 2

    Comment and Contribute


    (Maximum characters: 1200). You have characters left.



    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


    We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
    Thanks for your registration, follow us on our social networks to keep up-to-date