Recursion in livescript

I have been looking at JavaScript, and languages that compile to JS. Coffeescript was my first love in this domain, and I have very recently transitioned to LiveScript, just a few months ago.

Until now, I was not faced with a problem for which recursion is the most natural solution. A couple of days go, when I was writing this program to test for a palindromic number, a recursive implementation seemed the most sensible approach. LiveScript being my new found love, I fired up vim, and… blank.

I tried searching on google, but it turned up nothing useful. I spent almost half an hour reading livescript and prelude, to find these two solutions. Turns out that you can use the keyword function to create named functions, which get hoisted as usual with javascript.

function fib x
   if x<0 then throw new Error
   if x<3 then x
   else fib(x-1)+fib(x-2)

Or, you can use the fix function from prelude to do anonymous recursion. This is the first time I saw the beautiful Y combinator being used this way. And I had to read my old notes on Y combinators and Javascript call and apply to understand the magic of what is happening.

require! 'prelude-ls'.fix

fib = fix (fib) ->
   (x) ->
       if x < 0 throw new Error
       if x < 3 then x
       else fib(x-1) + fib(x-2)

Looks similar? Nope. Here, fix takes a function that returns a function as an argument, and returns a function that is the inner function, with the outer argument referring to the inner function itself. Did that make any sense? ūüėÄ

The beauty of this approach is that the function fib doesn’t get hoisted, it is just an ordinary variable assignment, not a named function, and we just did anonymous recursion!

Don’t know if the sound of it excites you, but I sure am excited. Recursion in an anonymous function? Isn’t that cool?

Actually, looks like one of the common applications of Y combinator is to do anonymous recursion, but I didn’t think of it earlier.

Detective Case study competition by STREE, an initiative of NSS IIT Delhi

Hello everyone. Just wanted to publicise a mailer I received. Find its full text below.
Inline images 1     Inline images 3
STREE, an initiative of NSS IIT Delhi
DETECTIVE Case Study Competition
On the occasion of¬†International Women’s Day¬†(8th March, 2014), STREE, an initiative of NSS-IIT Delhi brings to you a one-of-its-kind competition that lets you go through the complex process of analysing a case of sexual harassment. The competition aims at sensitising the participants to the subtle issue of sexual harassment at workplace through a fun-filled detective case study game.
Did you know that according to the The Sexual Harassment of Women at Workplace Act, 2013, every workplace is supposed to have a Sexual Harassment Complaints Committee to handle case of harassment. Moreover, the Committee is required to complete the inquiry within a time period of 90 days and send the report. Also, the employer or the District Officer are mandated to take action on the report within 60 days.
If these questions interest you, participate in the competition to explore this issue to the core.
Register in Teams of 3 at

(There are no restrictions on age, sex or institution on the team, though we prefer participation from college students)
  • Pre-seen material will be provided to the teams in bits and pieces starting from¬†6th March. (you may register later as well)
  • After going through the case study material, participants get an option to have a live chat with the characters in the case study with some restrictions, to be declared at the time of chat) on¬†8th of March. More details on this will be sent to the registered teams one day in advance.
  • The registered teams need to submit a short report of not more than 1000 words which contains their conclusion from the material and the chat session and also their recommendations.
    Deadline: 9th March, 2014 5:00 pm.
  • The participants will be judged on the basis of the way they investigate the case and also how sensitive they are towards the issue of Gender Equality and Sexual Harassment (which will be clear from the questions they ask during the chat session and also the report they submit).
Attractive Prizes to be Won
For more details, contact:
Stree Team
¬†— | |