Fibonacci series in haskell

So I was tired of doing (boring) stuff, and all – so I decided to take up a new challenge, the Project Euler. To sweeten the deal, I’ve decided that I’d use only Haskell to solve them.

The second question is to generate the sum of even terms of Fibonacci series. I’m going to show how the series is generated , and thus the following is – not – the solution to the problem. I’ve assumed that the highest term generated should be less than 15000, in this snippet.

takeWhile (\(x, y) -> x < 15000) (iterate (\(x, y) -> (x+y, x)) (1, 0))

Let me break it down –

1. \  syntax is used to define a lambda. So we define two lambdas in this snippet, one for incrementing the series, and other for checking

2. Iterate function is used to generate newer terms from existing terms by using a function.

3. takeWhile is used to pick out elements from a list till the condition is satisfied.

Now, the iterate function produces an infinite list, which is bounded by using takeWhile function.

Test run:

Prelude> takeWhile (\(x,y) -> x < 15000) (iterate (\(x, y) -> (x+y, x)) (1, 0))

[(1,0),(1,1),(2,1),(3,2),(5,3),(8,5),(13,8),(21,13),(34,21),(55,34),(89,55),(144,89),(233,144),(377,233),(610,377),(987,610),(1597,987),(2584,1597),(4181,2584),(6765,4181),(10946,6765)]

As you can see, a list of tuples of fibonacci’s series are generated.

That’s it for now. Hopefully I’ve written something that makes sense 😉

PS: Interestingly, wordpress.com’s source highlighting plugin does not support haskell – so if anybody is listening, please add it soon!

Standard

Enabling media url in development server – Django

To make the {{ media_url }} tag work in templates while using development server, you need to do some stuff, listed below.

1. Add the following to your urls.py

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)

Don’t forget to add from django.conf import settings

2. Add to TEMPLATE_CONTEXT_PROCESSORS in settings.py the following
'django.core.context_processors.media',

3. Edit the MEDIA_ROOT and MEDIA_URL, and set them up.

4. Make sure you are using RequestContext to pass the context to the template, ie

  return render_to_response('template.html', {}, context_instance=RequestContext(request))

That’s it, you should be able to use {{ media_url }} tag in templates using development server now. Thanks to folks at stackoverflow for the information.

Standard

Using *args and **kwargs in python

please read the update to this post : Using *args, reloaded!

So we’ve been actively working on our latest project in django – and I came across this weird syntax in views
def view_function(request, *args, **kwargs)

Digging in further, I found that *args stands for argument list, and **kwargs stands for keyword argument list ( well, only the asterik and double asterik matters, the names can be anything ). So, why is this useful again?

For example:

def foo(*args):
  for i, arg in enumerate(*args):
    print "Argument ", i, " : ", arg

and call it by

foo("a", "b", "c")

Simply put, we now have a way to call a function with arbitrary number of parameter. The function foo can be passed with any number of arguments.

So, that’s about *args. Now what is **kwargs? It is the keyword argument list – which means you can pass the keywords as well as their values as a dictionary.

For example:


class Foo(object):
  def __init__(self, value):
    print value

class DerivedFoo(Foo):
  def __init__(self, *args, **kwargs):
    print 'DerivedFoo'
    super(DerivedFoo, self).__init__(*args, **kwargs)

myFoo = DerivedFoo("Calling Foo through DerivedFoo")

The super keyword can be used to call the methods of superclass – in this case the init of class Foo. The parameters which are passed to inherited class can be passed to the base class as shown above. This can be used to extend the behavior of the base class, without knowing anything about base class.

Standard