Following the Odersky FP Course - Exercise 1

Hey there,

New to elixir and FP, except for some functional-ish javascript.

I’ve started Martin Odersky’s Scala course and figured I could do the problems in Elixir too, here’s the first.

http://elixirplayground.com?gist=840fa35e82a1e5b4aba419002788cfb4

Comments welcome on style and idomatic Elixir stuff.

1 Like

Note some Scala - Elixir differences

  • Scala runs on JVM so ti has some limitations, like recursion. You need to make @tailrec calls (The last call in recursion should be call to function). Erlang/Elixir is optimized for recursion calls.
  • Scala is mix OO + FP (more OO than FP).
  • Due to other JVM limitations Scala code can look sometimes ugly…

Anyway good luck with Scala :slight_smile:
In my opinion if you want to learn true functional language with static types try Haskell.

PS
I am not saying that Scala is bad language. There are some great stuff written in this (Apache Spark, Akka). But From my perspective learning Scala/Functional programming I would choose different language for start. There is to much OO/JVM noise that distracts from pure functional thinking. This is only my personal opinion.

2 Likes

My advice here is to stick to Scala if you are going through the Odesky course, because you will have a tough time not only learning new concepts but having the mental overhead of trying to convert into Elixir. Or better yet, just build Elixir programs!

I’ve found the best way that Works for MeTM is to build stuff first and get a feel of it. The FP stuff will come as you write more programs and hit into roadblocks and/or epiphanies/nirvana.

Besides, Elixir is already an FP language and will not allow you to do stuff that a mixed-paradigm language such as Scala will gladly allow.

But more importantly, welcome to Elixir & FP! It’s hard but stick with it! Finally, the code looks fine. I would have written it this way (I didn’t test this out, but I’m pretty sure it’s mostly legit):

defmodule Delist do
  def sum(list), do: sum(list, 0)
  def sum([], acc), do: acc
  def sum([h|t], acc), do: sum(t, acc + h)
end
2 Likes

Took that Martin Odersky course 3+ years ago.
I would recommend taking “Programming Languages” by Dan Grossman on Coursera first if you have the time/chance.
(https://www.coursera.org/learn/programming-languages)

It gives you elements of FP first in various languages and show you OO later. Very helpful to see how the 2 paradigms intersect and differs.

The unfortunate thing for Scala is being a big hybrid (“platypus”) language making it hard to stand on 2 sides of the paradigms(OO,FP) at once. It’s also easy fall into writing “smarts”, terse, abstruse FP code that is hard to read and consequently, maintainable that FP programmers can falls into. I learned this first hand from production code.
Elixir is a fresh air, just my personal opinion.

2 Likes

Ben! Been reading your book and love it so far :slight_smile: Thanks for the code comment. I never thought to do it in a polymorphic style, cool.

We’ll see how far I get on translating these from Scala. I like the idea of doing some analytics work with Apache Spark in FP style, which is why I want to learn it. Elixir definitely has more appeal as a community driven project, though.

1 Like

Scala is botb oop and fp does anyone find that strange and conflicting? Just wanted your thoughts!

Also, how does Scala handle memory? Like Elixir spawns new process… Do you guys know what Scala does? Thanks!

For me this is problem. I don’t like this aproach. I would prefer rather something like F#.[quote=“l_tonz, post:6, topic:1926”]
Also, how does Scala handle memory? Like Elixir spawns new process… Do you guys know what Scala does? Thanks!
[/quote]

You can have many different concurrency models in Scala:

If you are more interested I can recommend this book https://www.packtpub.com/application-development/learning-concurrent-programming-scala

1 Like

It sounds like Scala took all the concepts in OOP and FP and wrapped in in a language.

Hehe

Scala is a hybrid (“platypus”) language to provide a bridge to existing Java devs and ecosystem. This is both its strength and also weakness of the language. Providing 2 conflicting paradigms can end in codebase that is both confusing and messy to read/maintain, increasing TCO. Some folks uses OO at higher level and uses FP using a strategy of having functional cores, nuggets, or guts embedded within an object-oriented shell,
( Note: Scala also harder to learn and master so that good Scala devs can command high salaries in banks and financial institutions like SmallTalkers once did which contributed to its gradual down spiral.)

This may sound odd considering how well known anti-Java I am, but I used Scala for almost 4 years for various work, I actually very much enjoyed it, it made using the JVM tolerable. :slight_smile:

1 Like