Hacker News new | past | comments | ask | show | jobs | submit login
Interval sequences in Common Lisp (ahefner.livejournal.com)
12 points by nickb on Aug 20, 2008 | hide | past | favorite | 5 comments



From what I can tell, this provides two operations: interval and below. I'm not a Common Lisp user so I don't know the intricacies of CLOS but it seems to me that these functions can be written much more compactly, albeit without the extensive error reporting (in Scheme):

    (define (interval a b)
      (if (<= b a) '()
          (cons a (interval (+ a 1) b))))

    (define (below x)
      (if (<= x 1) '(0)
          (append (below (- x 1)) (list (- x 1)))))
What advantage does the wall of code in the article have over these two functions?


The main advantage that I see is that his class transparently works with any function expecting a 'sequence', which spec only defines as either a vector (i.e., 1-d array) or a list (i.e., linked list) while providing a some (extremely simple) lazy evaluation. I, for one, never realized that you could extend the sequence type in this way in SBCL.

For example, if he created an instance of his interval-sequence class with (below 100000000), he wouldn't actually have to allocate the memory for a hundred million element vector (or a 100 million cons cells). Instead, it would just use a handful of bytes to represent the start of the sequence, and the length.

By itself, it's fairly trivial. But, this sort of manipulation isn't part of the ansi spec so this post was just sort of letting us know what was possible (a 'hello world' of sorts).

I already have a few interesting ideas on how one could use this same technique to provide slightly smarter lazy evaluation, and some sort of time/duration manipulation stuff.


I'm a big python user, with some lisp coding in the past. I took a look at his code and I'm just wondering if this is any different than xrange() in python? For those that don't know, xrange is a generator of integer sequences... a way of iterating a "list" of N integers while only allocating space for 1 integer.

*note: This isn't a "python is better" argument, it's an "I'm surprised sbcl doesn't provide xrange type functionality, am I right in that assumption" argument.


It looked like xrange to me. Honestly, though, I can see why it's not in SBCL, because I don't actually find myself using xrange all that much. It's mostly stuff like:

  for unused in xrange(500):
    # perform some operation (500 times)
In Lisp, this is just:

  (dotimes (unused 500) ; perform some operation... )


The biggest difference is that your stores (interval 1 N) as an N-element list. Theirs stores it as an object with 2 slots, while still being a subtype of SEQUENCE so many built-in functions work as expected.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: