February 19, 2013

Set up Racket for Vim and mac Terminal

   
Racket is fairly great:), though the setting up procedure is kind of twisted. There is a .dmg installation file in the official website, which makes it quite straightforward to install in the Application folder. And, of course, the DrRacket app is awesome when I accidentally put mouse in one of the variables in the code, and then all its dependencies reveal via arrows.

But I am still used to writing code in vim and testing it in a parallel split window of iterm. I googled the solution, but cannot find a comprehensive one. So, I just list my solution here, which come from either stackoverflow or other persons' blogs.

1) vim syntax and highlight are out there ready for use.

2) Adding one command line to ~/.vimrc
 au BufRead,BufNewFile *.rkt set filetype=racket
3) Adding the path of executable racket to ~/.bach_profile.
export PATH=$PATH:/Applications/Racket\ v5.3.3/bin/
4) If using the existing file, one should delete the first line of code "#lang racket" if existed.

5) Optional. Installing rlwrap to make the repl more user friendly.

That is much of it.


cheers,

February 14, 2013

1st update in 2013

   
The issue came from my question in stackoverflow. Basically, it was a misplaced question, because the reason why my program would hit the "RuntimeError: maximum recursion depth exceeded" was that I just passed the sorted array by the first scenario to the second scenario. This caused the second one hit the "worse case" and exceed the "maximum recursion depth". Well... it was embarrassing to admit that I forgot dereferencing the variable...

But, anyway, the question concerning recursion in Python has been in my head for a while, even since I started learning Scheme. This time, the kind answer below my question shed lights on how to solve this kind of problem. Then I dug a little deeper and find more about tail recursion in Python.

The following is an example with problematic way to implement a recursion function:

def fac(n):
    return 1 if n < 2 else n * fac(n - 1)

> fac(3000)
> RuntimeError: maximum recursion depth exceeded

The right way to do this is to make a local helper function "tail recursive" with the help from anonymous function.

def fac(n):
    def f(n, acc):
        return acc if n < 2 else lambda: f(n - 1, acc * n)

    t = f(n, 1)
    while callable(t):
        t = t()
    return t

And an even elegant way is to use built-in function "reduce":

def fac3(n):
    return reduce(lambda x,y:x*y, xrange(1,n))
The latter two could deal with any input number. Tada~

A little extended reading about this topic:
Origins of Python's "Functional" Features
Tail Recursion Elimination
Tail Recursion Elimination in Python

Besides, studying Standard ML does give me much much more insights into both programming itself and programming in Python. Except the attractive tail recursion, Python has quite a few amazing features of functional programming.  In retrospect, now I appreciate more about what was listed in the "progression path", and of course, cannot wait to progress to Haskell.





cheers,

Days of our lives

Daisypath Anniversary tickers