From: Xcriber51
Subject: implementing the range op
Date: 
Message-ID: <6aaf70f20a51497854b53f0340d5d93b@localhost.talkaboutprogramming.com>
Hi

I'm trying to emulate the ".." operator known in some languages (Ada,
Haskell, etc.) in LISP, but being new to it, I'll settle for a function
named "range" for now. Here is my first attempt:

  (defun range (i n)
     (if (< i n) (cons i (range (+ i 1) n)) i))

Naturally, it isn't exactly working since when I type, for instance:

  (range 1 4)

I get 

  (1 2 3 . 4)

What's the deal with the "." there? What is going on?

Thanks!

-- K

From: Peder O. Klingenberg
Subject: Re: implementing the range op
Date: 
Message-ID: <ksmz83utg7.fsf@beto.netfonds.no>
"Xcriber51" <·······@[OMITTED]> writes:

> What's the deal with the "." there? What is going on?

A normal list ends with NIL as the cdr of the last cons cell.  You are
constructing a list where the final cons cell ends with your final
number.

  (defun range (i n)
     (if (< i n) (cons i (range (+ i 1) n)) (cons i nil)))

should work better.

However, if I were writing this function, I'd do something like

  (defun range (start stop)
     (loop for i from start to stop collect i))

Which iterates rather than recurses (not even tail recursing).

...Peder...
-- 
I wish a new life awaited _me_ in some off-world colony.
From: Peder O. Klingenberg
Subject: Re: implementing the range op
Date: 
Message-ID: <ksirirutcb.fsf@beto.netfonds.no>
·····@news.klingenberg.no (Peder O. Klingenberg) writes:

> "Xcriber51" <·······@[OMITTED]> writes:
>
>> What's the deal with the "." there? What is going on?
>
> A normal list ends with NIL as the cdr of the last cons cell.  You are
> constructing a list where the final cons cell ends with your final
> number.

Oh, and the "." is just the lisp printers way of telling you that the
cdr of that cons cell is something other than NIL.

...Peder...
-- 
Sl�v uten dop.
From: Peder O. Klingenberg
Subject: Re: implementing the range op
Date: 
Message-ID: <ksd58zut87.fsf@beto.netfonds.no>
·····@news.klingenberg.no (Peder O. Klingenberg) writes:

> "Xcriber51" <·······@[OMITTED]> writes:
>
>> What's the deal with the "." there? What is going on?
>
> A normal list ends with NIL as the cdr of the last cons cell.  You are
> constructing a list where the final cons cell ends with your final
> number.

Oh, and the "." is just the lisp printers way of telling you that the
cdr of that cons cell is something other than another cons cell or
NIL.

...Peder...
-- 
Sl�v uten dop.
From: Xcriber51
Subject: Re: implementing the range op
Date: 
Message-ID: <f4eda823cba53d0f97134c0c1933e9c5@localhost.talkaboutprogramming.com>
Heh... so that was it? I forgot cons nil? OK. You live and learn. 

The dot mystery is solved. Thanks.

Now as for the second version you suggest... erm, if I wanted 'Python', I
would have gone for it ;-]  but you see, I have an allergy for "keywords"
-- which is why I came to LISP (and which is why I'm likely to smart under
sufferings).

Sorry, I'm just drivelling. Never mind me. Thanks again.


Cheers
K
From: Zach Beane
Subject: Re: implementing the range op
Date: 
Message-ID: <m31wpfknbd.fsf@unnamed.xach.com>
"Xcriber51" <·······@[OMITTED]> writes:

> Now as for the second version you suggest... erm, if I wanted 'Python', I
> would have gone for it ;-]  but you see, I have an allergy for "keywords"
> -- which is why I came to LISP (and which is why I'm likely to smart under
> sufferings).

This sort of uninformed prejudice usually goes away after getting some
experience with Common Lisp. Informed prejudice is nicer.

Zach
From: Xcriber51
Subject: Re: implementing the range op
Date: 
Message-ID: <6769e5561228cfac9de07ad76b437184@localhost.talkaboutprogramming.com>
Guys, as I've hinted, I wasn't entirely serious. 

I humbly believe informed prejudice ain't better since the whole point of
having a prejudice is refusing to become informed.

And no, I DON'T like warm beer - yech, blech, pew... you're awful.

Look, I just like recursion, OK? I could run this function as "(range 1
10000)", and step through the stack manually and watch the thing unfold -
the way I could watch a couple of rotating tires before a car repair
shop.

Get it? Now spare your iterative nonsense. It ain't grabbin' me.

:-P


Cheers
-- K

P.S. I'm sure I'll appreciate your recommendations one day - a few years
of LISPing and tons of cold beer later. Until then just let me have my
precious prejudices. They're all I have (sob sob...)
From: Rahul Jain
Subject: Re: implementing the range op
Date: 
Message-ID: <87d58yhm83.fsf@nyct.net>
"Xcriber51" <·······@[OMITTED]> writes:

> Look, I just like recursion, OK? I could run this function as "(range 1
> 10000)", and step through the stack manually and watch the thing unfold -
> the way I could watch a couple of rotating tires before a car repair
> shop.

Uh... most people who want recursion want to explicitly NOT be able to
do that. They want the compiler to convert their recursion into an
iteration. SERIES takes a different angle on the issue and does
something similar to lazy evaluation, but compiles it as an iteration,
as well.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: =?utf-8?B?VGlhcm7DoW4gw5MgQ29ycsOhaW4=?=
Subject: Re: implementing the range op
Date: 
Message-ID: <m28xjmsp4a.fsf@cascade.local>
>>>>> "X" == Xcriber51  <·······@[OMITTED]> writes:
       X> Look, I just like recursion, OK? I could run this function
       X> as "(range 1 10000)", and step through the stack manually
       X> and watch the thing unfold - the way I could watch a couple
       X> of rotating tires before a car repair shop.

Recursion is nice -- tail recursion is nicer:

(defun range (start end &optional accum)
    (if (> start end)
	(nreverse accum)
	(range (1+ start) end (cons start accum))))

-- 
Tiarnán
From: Peder O. Klingenberg
Subject: Re: implementing the range op
Date: 
Message-ID: <kspscxq5gu.fsf@beto.netfonds.no>
········@yahoo.com (Tiarn�n � Corr�in) writes:

> (defun range (start end &optional accum)
>     (if (> start end)
> 	(nreverse accum)
> 	(range (1+ start) end (cons start accum))))

If you count backwards, you can avoid the nreverse at the end.  See my
DO-based version.

...Peder..., microoptimizations'r'us
-- 
This must be Thursday.  I never could get the hang of Thursdays.
From: Raffael Cavallaro
Subject: Re: implementing the range op
Date: 
Message-ID: <200610120204518930-raffaelcavallaro@pasdespamsilvousplaitmaccom>
On 2006-10-11 10:20:11 -0400, "Xcriber51" <·······@[OMITTED]> said:

> Look, I just like recursion, OK?

Oh no, that's next door - comp.lang.scheme - it's being hit on the head 
lessons in here ;^)
From: Rahul Jain
Subject: Re: implementing the range op
Date: 
Message-ID: <87hcyahmc2.fsf@nyct.net>
"Xcriber51" <·······@[OMITTED]> writes:

> Heh... so that was it? I forgot cons nil? OK. You live and learn. 
>
> The dot mystery is solved. Thanks.
>
> Now as for the second version you suggest... erm, if I wanted 'Python', I
> would have gone for it ;-]  but you see, I have an allergy for "keywords"
> -- which is why I came to LISP (and which is why I'm likely to smart under
> sufferings).
>
> Sorry, I'm just drivelling. Never mind me. Thanks again.

No, that's perfectly acceptable. Lisp allows you to use any paradigm you
want. You just have to ask for it. Maybe you want to use SERIES?

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Peder O. Klingenberg
Subject: Re: implementing the range op
Date: 
Message-ID: <ks3b9vt56p.fsf@beto.netfonds.no>
"Xcriber51" <·······@[OMITTED]> writes:

> Now as for the second version you suggest... erm, if I wanted 'Python', I
> would have gone for it ;-]  but you see, I have an allergy for "keywords"
> -- which is why I came to LISP (and which is why I'm likely to smart under
> sufferings).

Ok, but even if for some aesthetic reason I wanted to avoid LOOP, I'd
still do it iteratively instead of recursively:

(defun range (start stop)
  (do ((result nil)
       (i stop (1- i)))
      ((= i start) (push i result))
    (push i result)))

..Peder...
-- 
I wish a new life awaited _me_ in some off-world colony.
From: Espen Vestre
Subject: Re: implementing the range op
Date: 
Message-ID: <m1d58zxbx0.fsf@doduo.netfonds.no>
"Xcriber51" <·······@[OMITTED]> writes:

> Now as for the second version you suggest... erm, if I wanted 'Python', I
> would have gone for it ;-]  but you see, I have an allergy for "keywords"
> -- which is why I came to LISP (and which is why I'm likely to smart under
> sufferings).

Loop may be ugly, but IMHO refusing to use it is just stupid
self-punishment. But what do I know - maybe you prefer warm
beer too? ;)
-- 
  (espen)
From: Maciek Pasternacki
Subject: Re: implementing the range op
Date: 
Message-ID: <873b9rj02u.fsf@lizard.king>
On Prickle-Prickle, Bureaucracy 65, 3172 YOLD, Espen Vestre wrote:

>> Now as for the second version you suggest... erm, if I wanted 'Python', I
>> would have gone for it ;-]  but you see, I have an allergy for "keywords"
>> -- which is why I came to LISP (and which is why I'm likely to smart under
>> sufferings).
>
> Loop may be ugly, but IMHO refusing to use it is just stupid
> self-punishment.

If someone restricts you to pure ANSI, well, you're right.  Bot
otherwise using LOOP instead of ITERATE seems masochistic.

> But what do I know - maybe you prefer warm
> beer too? ;)

Even hot.  http://www.polishnews.com/fulltext/chef/2001/chef58_1.shtml
(as "mulled beer").  Tastes great with an egg stirred into it :)

-- 
__    Maciek Pasternacki <·······@japhy.fnord.org> [ http://japhy.fnord.org/ ]
`| _   |_\  / {  Any sufficiently complicated C or Fortran program contains an
,|{-}|}| }\/ad hoc informally-specified bug-ridden slow implementation of half
\/   |____/  of Common Lisp. }  ( Greenspun's 10th Rule of Programming )  -><-