Learning functional programming in scala

(-: Given I am nearing the end of my career as a full-time faculty member and am not a teacher of CS1 and CS2, I will leave initiation of any discussion of language usage in the early part of the curriculum to my current and future colleagues. We tend to go through such a discussion about once a decade—changing from Pascal to Java in 1999 (which I supported after introducing Java in what is now our CS3 course) and considering a change from Java to C/C++ (led by the systems-oriented faculty, which I opposed) in about 2008. Given the increases in size of our student body and faculty over the past decade and given our switch to a more lab-based and tool-intensive approach in CS1-2-3, a change would be more difficult now than the switch in 1999. … Given the increased use of Python in our upper-level and non-majors curricula, Python 3 would likely be the leading candidate for the primary teaching language if there was a switch. … And it probably some coordination with the community colleges in the State…and, hence, indirectly with the other public universities.

Although some of the regional employers of our students use Scala some, there are probably many more that use C#, Java, Python, JavaScript, or niche languages like ABAP. So there is no current push from outside for Scala. And whenever we discuss changes to the early curriculum, most instructors want there to be multiple textbook options.

My current sense is that Scala might could gain some momentum as a teaching language from data science applications like those in the Spark ecosystem, but in data science apps Python has a lot more wind to its back.

My recent teaching in our core curriculum has been in our Organization of Programming Languages course, which has 3rd and 4th year students. The past two years (Fall semester-only class) I have used Haskell and attempted an interpreter-building approach using my own courses materials (partial draft textbook). Unfortunately, getting students up to speed in FP/Haskell takes too long to do as much in the interpreter realm as I hoped. Would Scala be better for that? Maybe (at least if I allow a hybrid approach rather than pure FP).

As my students probably get tired of me saying, “it depends” … on a lot of circumstances that make a general statement difficult.

When I taught a lecture-based course from the Red Book to students whose common language was Java (before Java 8), I would combine the Red Book, with the answers to some of the exercises, some of my own examples, and background material on how Scala differed from the student’s likely previous Java experience. Items in the latter category included how Scala’s approach to inheritance differed from Java, variance, varieties of parameter passing (since most think just by value), algebraic data types, etc. The students who had strong programming backgrounds in Java or C# did well. Those with weaker programming backgrounds struggled. But this would likely be the same regardless of language used.

I prefer to teach FP using a language whose core is relatively pure that no student knows. Then the “playing field is level” and students cannot fall back on imperative thinking. However, students tend to be more motivated if they consider the language they are learning to be practical. They tend to see languages like Haskell (or those in the ML family) not to be practical because they don’t see those languages in job ads.

Agreed, in the general case ‘it depends’ is the right answer, but in this case I’m trying to provide an answer that’s suitable for the original asker based on what I know of his experience level and learning goals.

It also depends on a particular person’s learning style.

I tend to approach learning a new language first by reading quite a bit about the language (both discussion and code) and then “dive in” to one or more particular examples – usually reimplementing something (a kata?) that I have done in other languages. I tend to do lots of refactoring and create several different implementations as I learn more. I tend to immerse myself in the language for a few days or weeks. … But I have learned 30 or more languages since I first learned Fortran 45 years ago. Perhaps I did not learn my second and third languages (which happened to be PL/I and COBOL) quite that way.

Learning a different paradigm is, of course, more difficult. I found it a bigger effort to learn FP in Haskell-like languages in 1991 and to learn OO in C++ and Java in 1996. I had to do considerably more reading and programming than just to switch to a different imperative language.

I want to give priority to learning functional programming over learning Scala.

What does it mean to teach/learn “functional programming” versus “functional programming in Scala” … or language X, where X = Scala, Haskell, Scheme, etc.

As I have noted in other replies, I teach a functional programming course in which over the past few years I have used Haskell, Scala, or Elixir (and have also done short modules on FP in Lisp and Elm). I have considered using other languages such as OCAML, F#, JavaScript with the Ramda library, PureScript, etc. A long-time colleague (who is currently my Department Chair) asked me why I have different versions of the course for different languages. Why not just teach one course that is easy to use with different languages? It would make my life easier and make scheduling the course easier.

This colleague teaches courses like algorithms, AI, databases, etc., and does so in a language-independent manner. Her students might do the programming projects in different languages in a particular semester. This colleague also teaches courses in web programming and mobile apps, so I answered her by asking if she could teach a language/platform-independent course on mobile or web apps? … But I am not full satisfied with my answer!

What is functional programming? Here are a few different ideas that seem to be wrapped up in what various people call functional programming

  1. constructing programs as a set of pure (i.e. mathematical) functions

  2. using recursion instead of loops for doing something repeatedly

  3. using immutable data structures

  4. using first-class and higher-order functions

  5. using a strong static type system

  6. using algebraic data types and pattern matching

  7. using type classes (higher-kinded types?)

  8. issues related to eager and lazy evaluation of expressions

Although there are many common aspects of the Haskell and Scala versions of my course, I am not sure that it would make much sense yet to teach a language-independent FP course…or to write a useful language-independent FP textbook? Or am I not looking at this correctly?

Maybe we could use a FP pseudocode to cover points 1-4 above, but probably not the others very well. But learning a programming approach fully seems to “require” a specific language with an implementation for the best results.

Any thoughts?

I believe the OP said, as you quoted, he wants to ‘give priority to learning functional programming over learning Scala’. Not ‘learning functional programming in Scala’. I believe in this case the considerations are different. The question becomes, Is Scala a good language to teach functional programming? Could there be a better language?

The course I suggested uses SML and uses it very well indeed. At one point I had a lunch-and-learn group at a previous workplace where I adapted it to F#, and many of the concepts carried over. The same would have been true of OCaml or Elm. Many of the concepts (Hindley-Milner type inference with unification, information hiding with modules, the other things I listed above) would have carried over.

In Scala it’s harder to separate out these pure concepts from the artefacts left over from object-oriented style. It becomes harder to explain inference if the algorithm upcasts the branches of a conditional expression to their common supertype. Harder to explain why methods and functions are different. Why we have abstract classes for some things and traits for others. You mentioned that:

IMHO, it’s a bit easier if we can teach the new paradigm in a language without the artefacts of the old one.

We plan to focus more on an “algebraic” approach, i.e. focusing on the operators a structure like a Monad / Functor / Monoid etc. provides and what laws it obeys, instead of focusing on how to implement it. We will only implement only essential stuff and show usages of those functions instead, leaving other functions as optional exercise. From what I’ve already read, the Cats book seems to be more like that compared to the red book.

The “red book”, at least partly, works from concrete examples toward the abstract generalizations. The examples seek to build the intuition for abstract theory. The approach described above, at least partly, works in the reverse direction. It develops the abstraction as general theory and then gives the examples as applications of the theory. It is perhaps more typical of a mathematics course.

It will be interesting to see which approach works best. I suspect it will work well for some students and less well for others. Some students are very concrete learners and others are more comfortable with the “mathematics”.

I agree. A reason for that is that Scala is feature-rich and fairly clean, making it easier to know what you’re looking for when using another language.

I used SML in teaching for 10 years before switching to Scala. Students didn’t like SML because it was not a “real language” and the libraries were limited. But a great thing about it was that I didn’t mention refs or array at all (until the end), which forced students to think functionally. They had no mutable variables and therefore had no choice. If you find yourself fighting your Java instincts in Scala, maybe playing with SML can help you get over that.

The “red book”, at least partly, works from concrete examples toward the abstract generalizations. The examples seek to build the intuition for abstract theory. The approach described above, at least partly, works in the reverse direction. It develops the abstraction as general theory and then gives the examples as applications of the theory. It is perhaps more typical of a mathematics course.

It will be interesting to see which approach works best. I suspect it will work well for some students and less well for others. Some students are very concrete learners and others are more comfortable with the “mathematics”.

We already tried it in the last few lectures, as we deviated a bit from the book after the large problems with the State chapter. What we did was show a concrete problem, then introduce a monad that can solve it functionally, only explaining what its methods do, not how (a bit like what you would read in a doc comment). Then the students applied this to other examples and in the end we showed, how the monad and its methods were implemented. Most students found this approach easier, but as I said we also had a few math students, and some of our mandatory courses, e.g. theoretical CS, are also mostly a math course.

1 Like

Hello,

I just uploaded the first part of an introduction to the State Monad and I thought you might be interested in it.

Here it is: https://www.slideshare.net/pjschwarz/state-monad-212839068 - download for flawless quality.

Philip.

Any good resource on learning libraries like Cat,ScalaZ…for beginner?

I can’t speak much to scalaz – my experience is entirely with Cats. That said, Sam Halliday’s Functional Programming for Mortals is very well-reputed as a good way to get into scalaz.

There is also a “port” of that book to Cats, unsurprisingly named Functional Programming for Mortals with Cats – I haven’t read through that myself yet, but it’s sitting on my desktop for when I have some time.

For an absolute-beginner intro to functional programming (not based on either library), Creative Scala is specifically designed as a short, lightweight introductory course in programming with an FP orientation – it can help with the initial mindset.

And all that said, I personally learned Cats from its documentation, which, while not as complete as I might wish, is still an unusually good course in functional programming in its own right. Reading through the sections in “Type Classes” and then “Data Types” in order will teach you most of the essentials, and in many cases not just what they do but why they work that way, and how those types were derived. It’s hugely educational.

(And once you get through that, the documentation for cats-effect is the logical next step.)

2 Likes

Complementing @jducoeur my learning path for FP was:

Note, this is intended for the functional programming part, the first step is to learn and feel comfortable with the language itself.

3 Likes

https://alvinalexander.com/scala/functional-programming-simplified-book/

1 Like