[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Simple recursive functions in Lisp

William James

11/9/2015 7:55:00 PM

Frode Vatvedt Fjeld wrote:

> then I think it makes perfect sense to say e.g that
>
> (defun sum (list)
> (loop for x in list sum x))
>
> is vastly better than
>
> (defun sum (list)
> (if (null list)
> 0
> (+ (car list)
> (sum (cdr list)))))

Instead of CL (COBOL-Like), let's use a Lispy language.

MatzLisp (Ruby):

def sum array
array.reduce(0, :+)
end

--
In the latter case, arrests are made, not so much for what has been done, as
for what probably would be done. The latter is more for the preventive, and
less for the vindictive, than the former. --- A. Lincoln, Letter to Erastus
Corning, June 12, 1863
28 Answers

TibetanMonkey, the Beach Cruiser Philosopher

1/2/2013 5:15:00 AM

0

On Jan 1, 8:04 pm, Gunner <gunnera...@gmail.com> wrote:
> On Tue, 1 Jan 2013 08:45:22 -0800 (PST), "TibetanMonkey, the Beach
>
>
>
>
>
>
>
>
>
> Cruiser Philosopher" <comandante.ban...@yahoo.com> wrote:
> >On Jan 1, 1:30 am, "Ray Keller" <Lefta...@re.desperate.com> wrote:
> >> "Gunner" <gunnera...@gmail.com> wrote in message
>
> >>news:5r34e898ghi5qa8hjv9hmbp2f7747dgb91@4ax.com...
>
> >> > On Mon, 31 Dec 2012 08:10:42 -0800 (PST), "TibetanMonkey, the Beach
> >> > Cruiser Philosopher" <thetibetanmon...@gmail.com> wrote:
>
> >> >>On Dec 29, 12:53 am, "Ray Keller" <Lefta...@re.desperate.com> wrote:
> >> >>> "Gunner" <gunnera...@gmail.com> wrote in message
>
> >> >>>news:0o0td8ljiur2425nepgedi4mjljg999nnb@4ax.com...
>
> >> >>> > On Fri, 28 Dec 2012 13:28:33 -0800 (PST), Devils Advocaat
> >> >>> > <mankygo...@gmail.com> wrote:
>
> >> >>> >>On 28 Dec, 20:57, Ja...@nospam.com (Jason) wrote:
> >> >>> >>> In article <gavrd8tnc8vj7po3d5jcjesl84p2eje...@4ax.com>,
>
> >> >>> >>> gunnerNOS...@gmail.com wrote:
> >> >>> >>> > On Fri, 28 Dec 2012 08:33:44 -0800 (PST), "TibetanMonkey, the
> >> >>> >>> > Beach
> >> >>> >>> > Cruiser Philosopher" <thetibetanmon...@gmail.com> wrote:
>
> >> >>> >>> > >On Dec 27, 5:52 pm, Gunner <gunnera...@gmail.com> wrote:
> >> >>> >>> > >> On Thu, 27 Dec 2012 11:05:11 -0800 (PST), "TibetanMonkey, the
> >> >>> >>> > >> Beach
>
> >> >>> >>> > >> Cruiser Philosopher" <thetibetanmon...@gmail.com> wrote:
> >> >>> >>> > >> >(just missed this point)
>
> >> >>> >>> > >> >On Dec 26, 10:18 pm, Gunner <gunnera...@gmail.com> wrote:
> >> >>> >>> > >> >> On Wed, 26 Dec 2012 17:36:25 -0800 (PST), "TibetanMonkey,
> >> >>> >>> > >> >> the
> >> >>> >>> > >> >> Beach
>
> >> >>> >>> > >> >> Cruiser Philosopher" <thetibetanmon...@gmail.com> wrote:
>
> >> >>> >>> > >> >> >It sounds like the same reasoning for those who kill
> >> >>> >>> > >> >> >Americans,
> >> >>> >>> > >> >> >just
> >> >>> >>> > >> >> >because other Americans have invaded their country.
>
> >> >>> >>> > >> >> Really? Lots of IEDs used on cyclists? Really? Then perhaps
> >> >>> >>> > >> >> you
> >> >>> >>> > >> >> should be come a pedstrian.
>
> >> >>> >>> > >> >Not needed. The bumpers usually do the job. Have you seen
> >> >>> >>> > >> >cyclists
> >> >>> >>> > >> >killing drivers?
>
> >> >>> >>> > >> Actually..yes I have. Saw a cyclist cut off a car, who then
> >> >>> >>> > >> drove
> >> >>> >>> > >> into a high voltage power pole and the driver died while trying
> >> >>> >>> > >> to
> >> >>> >>> > >> leave the vehicle.
>
> >> >>> >>> > >> The cyclist was arrested.
>
> >> >>> >>> > >Well, let me tell you my story. I was spit in the face by an
> >> >>> >>> > >aggressive driver who didn't have a clue I had the right to be on
> >> >>> >>> > >the
> >> >>> >>> > >road. That right was in question because there's sign on every
> >> >>> >>> > >bridge
> >> >>> >>> > >in the area that says "walk bike across bridge."
>
> >> >>> >>> > So were you walking the bike or riding the bike?
>
> >> >>> >>> > >If I had a gun I would have shot him, which is why I don't carry
> >> >>> >>> > >a
> >> >>> >>> > >gun. I still prefer the police and the authorities to enforce the
> >> >>> >>> > >laws. I would also like the authorities to write signs that make
> >> >>> >>> > >sense, and bike lanes and sidewalks that connect. The last line
> >> >>> >>> > >of
> >> >>> >>> > >defense is my pepper spray, but that's kind of weak for an
> >> >>> >>> > >enraged
> >> >>> >>> > >driver with a 5 ton vehicle.
>
> >> >>> >>> > So you wont be killing all the motorists then?
>
> >> >>> >>> > >I simply try to avoid the road where the terrorists rule.
>
> >> >>> >>> > Good. You will certainly likely live longer.
>
> >> >>> >>> > Gunner
>
> >> >>> >>> > The methodology of the left has always been:
>
> >> >>> >>> > 1. Lie
> >> >>> >>> > 2. Repeat the lie as many times as possible
> >> >>> >>> > 3. Have as many people repeat the lie as often as possible
> >> >>> >>> > 4. Eventually, the uninformed believe the lie
> >> >>> >>> > 5. The lie will then be made into some form oflaw
> >> >>> >>> > 6. Then everyone must conform to the lie
>
> >> >>> >>> The world would be a better place if you had shot that nut case that
> >> >>> >>> spit
> >> >>> >>> on you.
>
> >> >>> >>So you consider shooting someone for spitting on you a reasonable
> >> >>> >>reaction do you?
>
> >> >>> > Depends on if they were HIV positive or had hemmoragic fever or
> >> >>> > something equally fatal. You do know that at the least..its
> >> >>> > assault..and if the spitter has a possibly lethal disease..its
> >> >>> > considered a felony...right?
>
> >> >>> >>Tell me, do you think with an attitude like that you should be allowed
> >> >>> >>to own firearms and ammunition?
>
> >> >>> > Considering is far far different than Doing. Now we all understand
> >> >>> > that to Leftwing mental cases..there is little difference in the
> >> >>> > two..but for normal people.....shrug
>
> >> >>> >>> You have a right to ride a bike in the bike lane or walk your bike
> >> >>> >>> in the bike lane. I once had a flat tire on a bike and had to walk
> >> >>> >>> it
> >> >>> >>> home
> >> >>> >>> and no one complained or spit on me.
>
> >> >>> > The methodology of the left has always been:
>
> >> >>> > 1. Lie
> >> >>> > 2. Repeat the lie as many times as possible
> >> >>> > 3. Have as many people repeat the lie as often as possible
> >> >>> > 4. Eventually, the uninformed believe the lie
> >> >>> > 5. The lie will then be made into some form oflaw
> >> >>> > 6. Then everyone must conform to the lie
>
> >> >>> Nothing is more fun than car-dooring a dickhead bicyclist that holds up
> >> >>> traffic and fucks with drivers<VBG>
>
> >> >>You'll get more publicity if you start shooting them at random. Then
> >> >>shoot yourself.
>
> >> > Publicity?  Who wants publicity? Oh..thats right..you bike Nazis do.
>
> >> >>I'll be watching the news.
>
> >> > Id be watching the front windows if I were you....
>
> >> > Gunner
>
> >>  The little dickhead bicyclist got up from the pavement cussing and
> >> threatening  till I steped out of the truck and stood up...then he shit
> >> himself down both legs...lots of laughter and cheers from the crowd at the
> >> buss stop watching<VBG>
>
> >I think you should be fighting people your size. I bet you wouldn't
> >pick on him if you were riding a bike as well. I never had issues with
> >other cyclists. The bullies go and buy a truck to intimidate
> >everybody.
>
> Actually..the bullies ride the bicycles, in my experience.

Not much a cyclist can do against a man behind 5 tons of steel.


Kaz Kylheku

11/9/2015 8:17:00 PM

0

On 2015-11-09, WJ <w_a_x_man@yahoo.com> wrote:
> Frode Vatvedt Fjeld wrote:
>
>> then I think it makes perfect sense to say e.g that
>>
>> (defun sum (list)
>> (loop for x in list sum x))
>>
>> is vastly better than
>>
>> (defun sum (list)
>> (if (null list)
>> 0
>> (+ (car list)
>> (sum (cdr list)))))
>
> Instead of CL (COBOL-Like), let's use a Lispy language.

This dumb asshole's argument is fallacious because Common Lisp has reduce:

(reduce #'+ list)

> MatzLisp (Ruby):
>
> def sum array
> array.reduce(0, :+)
^
Lisp reduce doesn't need the explicit 0 seed value, because of its
intelligently designed + function:

(+) -> 0
(+ 1) -> 1
(+ 1 2) -> 3

No language that substitutes arrays for lists, and doesn't support
a convenient representation of code as data, can be called Lispy,
except by a complete imbecile.

> end

> array.reduce(0, :+)

Elsewhere you've been using &:function, now you have :+. What
happened to the &?

This is worse than Common Lisp's #' because it is inconsistent.

Though #' is criticized by some users of Lisp-1 dialects, at least
it's always #'. It isn't #' when applied to, say, the evenp
function, and something else when applied to the + function.

A Lispy language treats symbols consistently; it doesn't have
reserved keywords.

Marco Antoniotti

11/10/2015 9:22:00 AM

0

On Monday, November 9, 2015 at 9:16:51 PM UTC+1, Kaz Kylheku wrote:
> On 2015-11-09, WJ <w_a_x_man@yahoo.com> wrote:
> > Frode Vatvedt Fjeld wrote:
> >
> >> then I think it makes perfect sense to say e.g that
> >>
> >> (defun sum (list)
> >> (loop for x in list sum x))
> >>
> >> is vastly better than
> >>
> >> (defun sum (list)
> >> (if (null list)
> >> 0
> >> (+ (car list)
> >> (sum (cdr list)))))
> >
> > Instead of CL (COBOL-Like), let's use a Lispy language.
>
> This dumb asshole's argument is fallacious because Common Lisp has reduce:
>
> (reduce #'+ list)
>
> > MatzLisp (Ruby):
> >
> > def sum array
> > array.reduce(0, :+)
> ^
> Lisp reduce doesn't need the explicit 0 seed value, because of its
> intelligently designed + function:
>
> (+) -> 0
> (+ 1) -> 1
> (+ 1 2) -> 3
>
> No language that substitutes arrays for lists, and doesn't support
> a convenient representation of code as data, can be called Lispy,
> except by a complete imbecile.
>
> > end
>
> > array.reduce(0, :+)
>
> Elsewhere you've been using &:function, now you have :+. What
> happened to the &?
>
> This is worse than Common Lisp's #' because it is inconsistent.
>
> Though #' is criticized by some users of Lisp-1 dialects, at least
> it's always #'. It isn't #' when applied to, say, the evenp
> function, and something else when applied to the + function.
>
> A Lispy language treats symbols consistently; it doesn't have
> reserved keywords.

Tut tut tut Kaz :) Feeding trolls this morning? :)

Cheers
--
MA

Carlos

11/10/2015 6:39:00 PM

0

[Kaz Kylheku <kaz@kylheku.com>, 2015-11-09 20:16]
[...]
> (reduce #'+ list)
>
> > MatzLisp (Ruby):
> >
> > def sum array
> > array.reduce(0, :+)
> ^
> Lisp reduce doesn't need the explicit 0 seed value, because of its
> intelligently designed + function:
>
> (+) -> 0
> (+ 1) -> 1
> (+ 1 2) -> 3

Lisp reduce doesn't need the seed value because, if none is provided,
it starts applying the function to the first two values in the list. It
has nothing to do with the intelligent design of +. (Think about 'reduce
and the intelligent design of /).

Ruby is the same:

[1, 2].reduce(:+)
=> 3

> No language that substitutes arrays for lists, and doesn't support
> a convenient representation of code as data, can be called Lispy,
> except by a complete imbecile.
>
> > end
>
> > array.reduce(0, :+)
>
> Elsewhere you've been using &:function, now you have :+. What
> happened to the &?
>
> This is worse than Common Lisp's #' because it is inconsistent.
>
> Though #' is criticized by some users of Lisp-1 dialects, at least
> it's always #'. It isn't #' when applied to, say, the evenp
> function, and something else when applied to the + function.
>
> A Lispy language treats symbols consistently; it doesn't have
> reserved keywords.

This is off-topic, but since almost everybody has these threads hidden
anyway...

In Ruby, &:x isn't the equivalent to (function 'x), as you seem to have
"discovered" some messages ago. `:x' is a symbol, all right, but `&' is
just part of the method call syntax. Used on a symbol, it creates a
block (which calls the function denoted by the symbol) and attaches it
to the method call. This:

a.reduce(&:+)

is the same as this:

a.reduce { |a, b| a + b }

Most higher order functions accept either a symbol or a block; if there
is any inconsistence on how they are used, it's in the hands of the
user.

Ruby is no Lisp, and nobody pretends it is. You shouldn't criticize it
without knowing it, and just based on the claims of somebody who is just
starting to learn it (since a couple of years ago anyway).

--

Kaz Kylheku

11/10/2015 7:31:00 PM

0

On 2015-11-10, Carlos <angus@quovadis.com.ar> wrote:
> [Kaz Kylheku <kaz@kylheku.com>, 2015-11-09 20:16]
> [...]
>> (reduce #'+ list)
>>
>> > MatzLisp (Ruby):
>> >
>> > def sum array
>> > array.reduce(0, :+)
>> ^
>> Lisp reduce doesn't need the explicit 0 seed value, because of its
>> intelligently designed + function:
>>
>> (+) -> 0
>> (+ 1) -> 1
>> (+ 1 2) -> 3
>
> Lisp reduce doesn't need the seed value because, if none is provided,
> it starts applying the function to the first two values in the list.

And what are those two values, if the list is empty?

(reduce #'+ ()) -> 0

Do you think reduce knows anything about zero?

> It has nothing to do with the intelligent design of +.

Take a closer look.

> Ruby is the same:
>
> [1, 2].reduce(:+)
> => 3

How does it handle:

[].reduce(:+)

> This is off-topic, but since almost everybody has these threads hidden
> anyway...
>
> In Ruby, &:x isn't the equivalent to (function 'x), as you seem to have

That should be (function x).

> "discovered" some messages ago.

It's some blurb you have to add in front of a function to obtain it as a value
that you can pass, so it's a "moral equivalent".

> `:x' is a symbol, all right, but `&' is

If :x is the symbol, what happened to x?

This smacks of nonsense, like LOOP's string equivalence which lets you use
"for or :for", but placed in an unfortunately central and prominent place
in the language.

> just part of the method call syntax.

Kludge.

> Used on a symbol, it creates a
> block (which calls the function denoted by the symbol) and attaches it
> to the method call.

A whole fucking lambda generated by & just to call plus!

Ironically, in some past postings WJ chided some people for using exactly
such a lambda instead of just #'+ (when in fact the point of the original
posting was to illustrate some lambda usage).

Now we find that Ruby's cluster-fucked equivalent of #'+ generates a lambda.

> a.reduce(&:+)
>
> is the same as this:
>
> a.reduce { |a, b| a + b }

All misinventions of a demented amateur.

Carlos

11/10/2015 8:33:00 PM

0

[Kaz Kylheku <kaz@kylheku.com>, 2015-11-10 19:31]
> On 2015-11-10, Carlos <angus@quovadis.com.ar> wrote:
> > [Kaz Kylheku <kaz@kylheku.com>, 2015-11-09 20:16]
> > [...]
> >> (reduce #'+ list)
> >>
> >> > MatzLisp (Ruby):
> >> >
> >> > def sum array
> >> > array.reduce(0, :+)
> >> ^
> >> Lisp reduce doesn't need the explicit 0 seed value, because of its
> >> intelligently designed + function:
> >>
> >> (+) -> 0
> >> (+ 1) -> 1
> >> (+ 1 2) -> 3
> >
> > Lisp reduce doesn't need the seed value because, if none is
> > provided, it starts applying the function to the first two values
> > in the list.
>
> And what are those two values, if the list is empty?
>
> (reduce #'+ ()) -> 0
>
> Do you think reduce knows anything about zero?

It should signal, or return either nil or (values). 0 is wrong. Here the
"intelligent design" of + plays against you.

>
> > It has nothing to do with the intelligent design of +.
>
> Take a closer look.
>
> > Ruby is the same:
> >
> > [1, 2].reduce(:+)
> > => 3
>
> How does it handle:
>
> [].reduce(:+)

Returns nil, of course. An exception would be ok, too.

>
> > This is off-topic, but since almost everybody has these threads
> > hidden anyway...
> >
> > In Ruby, &:x isn't the equivalent to (function 'x), as you seem to
> > have
>
> That should be (function x).

You're right. I almost never use function and it shows.

So, actually it's better to pair &:x with (fdefinition 'x), although
they aren't equivalent (as already described).

>
> > "discovered" some messages ago.
>
> It's some blurb you have to add in front of a function to obtain it
> as a value that you can pass, so it's a "moral equivalent".
>
> > `:x' is a symbol, all right, but `&' is
>
> If :x is the symbol, what happened to x?

¿? Nothing. x is a placeholder, any symbol name. In Ruby, symbol
literals start with ":" and evaluate to themselves. It's a syntax
issue.

> This smacks of nonsense, like LOOP's string equivalence which lets
> you use "for or :for", but placed in an unfortunately central and
> prominent place in the language.

Not only you got it wrong, but what you are describing is Lisp.
Sometimes you quote... (fdefinition 'x), sometimes you don't (function
x).

> > just part of the method call syntax.
>
> Kludge.

Just syntactic sugar to avoid writing an unnecessary wrapping block.

>
> > Used on a symbol, it creates a
> > block (which calls the function denoted by the symbol) and attaches
> > it to the method call.
>
> A whole fucking lambda generated by & just to call plus!

Nobody does that (reduce accepts a symbol, remember?). It just seems WJ
learned about it last month. But in any case, I'd guess it's optimized
and the wrapping lambda is simplified away.

>
> Ironically, in some past postings WJ chided some people for using
> exactly such a lambda instead of just #'+ (when in fact the point of
> the original posting was to illustrate some lambda usage).
>
> Now we find that Ruby's cluster-fucked equivalent of #'+ generates a
> lambda.
>
> > a.reduce(&:+)
> >
> > is the same as this:
> >
> > a.reduce { |a, b| a + b }
>
> All misinventions of a demented amateur.

Ha. Ruby is excellent.
--

Pascal J. Bourguignon

11/10/2015 8:52:00 PM

0

Carlos <angus@quovadis.com.ar> writes:

>> And what are those two values, if the list is empty?
>>
>> (reduce #'+ ()) -> 0
>>
>> Do you think reduce knows anything about zero?
>
> It should signal, or return either nil or (values). 0 is wrong. Here the
> "intelligent design" of + plays against you.

Of course not. Requiring that (reduce '+ ()) return nil is a dumb
specification.

Ask any mathematician,

Σ x = 0
xâ??â??

and

Π x = 1
xâ??â??

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Barry Margolin

11/10/2015 9:11:00 PM

0

In article <87mvul4lx2.fsf@kuiper.lan.informatimago.com>,
"Pascal J. Bourguignon" <pjb@informatimago.com> wrote:

> Carlos <angus@quovadis.com.ar> writes:
>
> >> And what are those two values, if the list is empty?
> >>
> >> (reduce #'+ ()) -> 0
> >>
> >> Do you think reduce knows anything about zero?
> >
> > It should signal, or return either nil or (values). 0 is wrong. Here the
> > "intelligent design" of + plays against you.
>
> Of course not. Requiring that (reduce '+ ()) return nil is a dumb
> specification.

Right. If a function is associative and commutative, (reduce #'fun ())
should return the same thing as (fun), which should be the function's
identity value. The idea is that

(fun (fun a b c) (fun x y z)) == (fun a b c x y z) == any other grouping
of the arguments. This includes the limiting cases

(fun (fun) (fun a b c x y z))
(fun (fun a b c x y z) (fun))

And by induction, thit's also equivalent to

(fun (fun (fun (fun (fun (fun a (fun)) b) c) x) y) z)

which is what (reduce #'fun (list a b c x y z)) is.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Kaz Kylheku

11/10/2015 9:26:00 PM

0

On 2015-11-10, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> Carlos <angus@quovadis.com.ar> writes:
>
>>> And what are those two values, if the list is empty?
>>>
>>> (reduce #'+ ()) -> 0
>>>
>>> Do you think reduce knows anything about zero?
>>
>> It should signal, or return either nil or (values). 0 is wrong. Here the
>> "intelligent design" of + plays against you.
>
> Of course not. Requiring that (reduce '+ ()) return nil is a dumb
> specification.
>
> Ask any mathematician,
>
> Σ x = 0
> xâ??â??
>
> and
>
> Π x = 1
> xâ??â??

Moreover, given that (+) -> 0, and (apply #'+ ()) must necessarily
be equivalent to (+), why would we want (reduce #'+ ()) to be
gratuitously different from (apply #'+ ())?

Carlos

11/10/2015 9:49:00 PM

0

["Pascal J. Bourguignon" <pjb@informatimago.com>, 2015-11-10 21:52]
> Carlos <angus@quovadis.com.ar> writes:
>
> >> And what are those two values, if the list is empty?
> >>
> >> (reduce #'+ ()) -> 0
> >>
> >> Do you think reduce knows anything about zero?
> >
> > It should signal, or return either nil or (values). 0 is wrong.
> > Here the "intelligent design" of + plays against you.
>
> Of course not. Requiring that (reduce '+ ()) return nil is a dumb
> specification.

Well, in Ruby, + is "+ the binary operator", not "sigma disguised as
+". Maybe "sum" would have been a better name for the latter?

>
> Ask any mathematician,
>
> S x = 0
> x?Ø
>
> and
>
> ? x = 1
> x?Ø

Yeah, but they won't say that " + = 0".
--