Classical FP problem in python : Hamming problem

Francis Girard francis.girard at free.fr
Thu Jan 27 13:54:46 EST 2005


Le jeudi 27 Janvier 2005 10:30, Nick Craig-Wood a écrit :
> Francis Girard <francis.girard at free.fr> wrote:
> >  Thank you Nick and Steven for the idea of a more generic imerge.
>
> You are welcome :-)  [It came to me while walking the children to school!]
>

Sometimes fresh air and children purity is all what it takes. Much better than 
coffee, cigarrette and flat screen.

> [snip]
>
> >  class IteratorDeiterator:
> >    def __init__(self, iterator):
> >      self._iterator = iterator.__iter__()
> >      self._firstVal = None ## Avoid consuming if not requested from
> > outside ## Works only if iterator itself can't return None
>
> You can use a sentinel here if you want to avoid the "can't return
> None" limitation.  For a sentinel you need an object your iterator
> couldn't possibly return.  You can make one up, eg
>

Great idea. I'll use it.

>        self._sentinel = object()
>        self._firstVal = self._sentinel
>
> Or you could use self (but I'm not 100% sure that your recursive
> functions wouldn't return it!)
>
> >    def __iter__(self): return self
> >
> >    def next(self):
> >      valReturn = self._firstVal
> >      if valReturn is None:
>
> and
>
>        if valReturn is self._sentinel:
> >        valReturn = self._iterator.next()
> >      self._firstVal = None
>
>        self._firstVal = self._sentinel
>
> etc..
>
> [snip more code]
>
> Thanks for some more examples of fp-style code.  I find it hard to get
> my head round so its been good exercise!

Introduction to functional programming
Richard Bird and Philip Wadler
Prentice Hall
1988

This is the very best intro I ever read. The book is without hype, doesn't 
show its age and is language neutral.
Authors are world leaders in the field today. Only really strong guys have the 
kindness to do understandable introductions without trying to hide the 
difficulties (because they are strong enough to face them with simplicity).

It's been a real pleasure.

Regards,

Francis Girard
FRANCE


> --
> Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick




More information about the Python-list mailing list