21

What would be a good first functional programming project?

I am looking to finish with a bang on my bachelor project and I want to finish it with a functional programming project. We didn't study it in school but we are encouraged to try to learn new things ourselves, and I thought learning a new paradigm not only a new language would be a great exercise for me, and probably something interesting for the teachers to see too.

The problem is I want to start a bit ahead of time on my project to take advantage of the summer holiday and get a better understanding on functional programming, and get familiar with one language.

What would you say would be a good practice project that is a bit challenging, but also lets me learn the language, paradigm etc. And then what would you think would be a good project (a more advanced one) for my bachelor project?

Suggestions for the best language to start with when going into functional programming would be appreciated too.

Meme
  • 313
  • 1
  • 2
  • 4
  • 1
    "What language should I choose" and project selection questions are both off-topic here. Please see the [FAQ] and [this meta discussion](http://meta.programmers.stackexchange.com/questions/2308/ideas-about-a-project-and-guidance-on-career-enhancement-arent-off-topic) for further details. – Adam Lear Sep 23 '11 at 15:33

7 Answers7

11

It's generally easier if you go for something relatively mathematical or logical - functional programming languages are generally well suited for applications that represent some form of transformation from a given input to an output.

Some ideas, in rough order of difficulty:

  • Genetic algorithms - write a program that evolves solutions to a particular task where the solutions are represented in a simple DSL. I've had fun before building little bots that hunt for food in a 2D grid and evolve different kinds of strategies

  • Parsing combinators - build a parser combinator library that enables you to construct a parser for an arbitrary language using higher order functions.

  • If you really want a challenge, you can try writing a computer game... be aware that this is a tricky task since games have a huge amount of mutable state which can be tricky to manage in a functional programming style. Expect to learn more than you ever wanted to know about monads etc.....

I'd recommend Clojure as a pragmatic functional language. I've been using it for about 18 months now and I'm extremely happy with the choice. Main reasons are:

  • Concurrency - Clojure has an amazing STM system that in my view makes it the best language in the world at the moment for multi-core concurrency. See the video at: http://www.infoq.com/presentations/Value-Identity-State-Rich-Hickey if you want to understand why this is so special
  • It's a Lisp - so because of the "code is data" philosophy it is fantastic for macro-based metaprogramming (programs that write programs, genetic algorithms etc.)
  • Pragmatic functional programming - Clojure style is very functional (lots of emphasis on higher order functions, lazy sequences etc.) but it isn't totally pure like Haskell. There are lots of nice tools to handle mtable state and side efects
  • Dynamic - Clojure is a dynamic language by default. I find this a big boost for productivity. However, you can optionally add static type hints later if you want the performance benefits of static typing.
  • Fully compiled - Clojure code is always complied (even if you do an "eval") so you get pretty decent performance - certainly better than most dynamic languages I've used.
  • You get access to all the libraries and tools in the JVM ecosystem for free. So unlike academic languages that have very limited libraries available, you can access anything in the Java universe pretty easily

You can try out the basics very easily with:

  • The 4Clojure online problem set - the early problems serve as a good "learning by doing" introduction to the Clojure language
  • the online Clojure REPL at http://www.try-clojure.org/
mikera
  • 20,617
  • 5
  • 75
  • 80
  • I chose your answer for the question because it answered all my questions. Thank you very much for taking the time. – Meme May 27 '11 at 15:10
  • Haskell has STM too ;( – alternative May 27 '11 at 21:01
  • @mikera: For working with the JVM, have you tried ABCL? I tried it out and made only a small example but I have almost no experience with it. (I also have a Clojure book in my todo list, but this is another story, I am curious if you have any experience working with ABCL and Java). – Giorgio May 11 '13 at 23:03
  • @Giorgio: ABCL seems like it's a decent implementation of Common Lisp on the JVM. OTOH Clojure has the advantages of being designed for the JVM, incorporating a lot more "modern" innovations, and having more community momentum. I think it really comes down to whether you care about maintaining Common Lisp backwards compatibility or not. – mikera May 12 '13 at 01:56
10

Before you start thinking about specific projects, start with learning the basics of functional programming so you can have a good idea of the types of projects that would be a good fit.

The best place to get started is probably The Structure and Interpretation of Computer Programs (SICP), which is based on the Scheme dialect of Lisp. This is a classic CS text, and the full text is available online (link provided).

If you want to get fancy, and use a more modern functional language that targets the JVM, take a look at Clojure. There's even an adapted version of SICP specifically for Clojure.

Going over the SICP text, you'll get an idea of why functional programming is so well-suited to certain types of tasks, and the exercises might inspire a full-fledged project. If you choose to go the Clojure route, and want to examine some existing projects, there are some good links here.

Jason Lewis
  • 2,113
  • 13
  • 18
4

Quantitative analysis

If you find finance somewhat interesting doing some quant stuff with functional programming is a good match since it's very algorithmic. I'm talking portfolio theory and things like sharpe and sortino ratios etc. Make an app that analyses a fund's returns and gives different statistics, diagrams etc.

I'd recommend F# only because I think it's an easy functional language to get started in, has good tools and good framework backing it. Other alternatives are lisp and clojure but they're a bit harder to learn.

Homde
  • 11,104
  • 3
  • 40
  • 68
  • At high level, it is indeed true that you can do a lot of stuff in a functional manner. People have used functional languages to describe exotic option payoffs, for example. But as you go down to the ground level stuff, like calculating PCA, solving nonlinear equations, etc., the algorithms become increasingly iterative and you're better off coding them in the old-style way. – quant_dev May 27 '11 at 11:40
  • 1
    I wouldn't say that, in my experience quantiative analysis is all about running algorithms over series. I've implemented quant stuff in C# and it was very much aligned with LINQ, at least for the portfolio theory kind of stuff – Homde May 27 '11 at 12:19
  • What about the implementations of algorithms themselves? – quant_dev May 27 '11 at 12:29
  • I see no problem in most instances, do you? Take calculating a sharpe ratio, first you calculate an annualized return from a returns series, then you use that value with a risk free rate and volatility: (annualizedReturn - riskFreeRate) / volatility. Nothing a functional language couldn't handle – Homde May 27 '11 at 12:59
  • What about pricing model calibration? – quant_dev May 27 '11 at 14:04
  • can you point to some example code? – Homde May 27 '11 at 14:13
  • E.g. consider implementing LMM calibration in a purely functional model. To keep things simple, we can assume log-normal LMM and only calibrate to caplets and swaptions. – quant_dev May 28 '11 at 20:55
  • @Homde Is Scala good to implement your example? –  Mar 26 '17 at 02:43
4

For a head-start, you could try to implement a little blackjack game in F#. This is a homework project assigned during this short video tutorial. Solution is also provided on the net (and in one of the videos).

knb
  • 747
  • 1
  • 8
  • 16
3

You could write an interpreter for scheme or lisp with OCaml.

3

Which functional language were you thinking of. Each one has different characteristics. The one language that made the largest impact on me was Haskell and I would suggest you do the same.

  • I was thinking of F# honestly mainly because in Denmark Microsoft everything is popular, but the more I read different opinions, I am doubting this decision. I want a language that will help me learn the paradigm mainly, and after I am sure it's not hard to learn another one if I need or want to. – Meme May 27 '11 at 11:47
  • Strongly depends on where you are and what you do. For us Microsoft programming technologies are useless as they don't support our main platform. But I digress: if you want to use F#, great, but then open a new F# specific question. –  May 28 '11 at 06:32
  • I don't want to use that. I haven't taken a decision yet. The question about the language to use was a secondary one anyway. – Meme May 28 '11 at 08:55
  • 1
    In that case, find a project and then ask for recommendations how to solve it. E.g. Prolog is great for decision tree searching. Haskell is great for pattern matching. Lisp is great for constructing and manipulating Lisp programs at runtime depending on your data. –  May 28 '11 at 09:04
1

You could also contribute to some open source project.

For example, in the Frege programming language project, there is much work to do. For instance, you could care about porting base Haskell libraries. This would make you sort of a Haskell expert while you work on it.

Ingo
  • 3,903
  • 18
  • 23