Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Remember: Silly is a state of Mind, Stupid is a way of Life. -- Dave Butler


devel / comp.lang.lisp / Re: Lisp Beginner looking for some help with creating a function.

SubjectAuthor
* Lisp Beginner looking for some help with creating a function.ME Jones
+- Re: Lisp Beginner looking for some help with creating a function.Spiros Bousbouras
+- Re: Lisp Beginner looking for some help with creating a function.Ben Bacarisse
`* Re: Lisp Beginner looking for some help with creating a function.Spiros Bousbouras
 `* Re: Lisp Beginner looking for some help with creating a function.Ben Bacarisse
  `- Re: Lisp Beginner looking for some help with creating a function.Spiros Bousbouras

1
Lisp Beginner looking for some help with creating a function.

<cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17225&group=comp.lang.lisp#17225

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:ae9:f309:0:b0:6fa:8b0b:10c9 with SMTP id p9-20020ae9f309000000b006fa8b0b10c9mr5937854qkg.732.1668282157696;
Sat, 12 Nov 2022 11:42:37 -0800 (PST)
X-Received: by 2002:a05:6830:1419:b0:66c:a811:d996 with SMTP id
v25-20020a056830141900b0066ca811d996mr3895679otp.45.1668282157430; Sat, 12
Nov 2022 11:42:37 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.lisp
Date: Sat, 12 Nov 2022 11:42:37 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=67.185.73.139; posting-account=z6W7sQoAAADU1WINpl0JBaSWNNUw5WQ0
NNTP-Posting-Host: 67.185.73.139
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>
Subject: Lisp Beginner looking for some help with creating a function.
From: markjones@shawday.com (ME Jones)
Injection-Date: Sat, 12 Nov 2022 19:42:37 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1771
 by: ME Jones - Sat, 12 Nov 2022 19:42 UTC

Hello,
I am trying to write a function that returns the largest atom in a list of any depth. I am very new to lisp and learning.
I do not want to use the built-in max function and am trying to create a simple function which takes a list of integers and returns the largest one. Here is my code so far, any help is appreciated, thanks.

;return the largest value greater than 0 in a list at any level of lists of integers.
(defun largest_atom(L)
(cond ((null L) 0)
((atom(car L))(cons(car L)(largest_atom(cdr L))))
(if (< a b)
b
a
)
)
) ;test
;returns 0 if the list is empty.
;(largest_atom '())
;should return 0

;test
;(largest_atom '((1 2) 3 ( 2 3 (1 9))))
;should return 9

Re: Lisp Beginner looking for some help with creating a function.

<g4iN+w0SDq7=VQgmx@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17226&group=comp.lang.lisp#17226

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.lisp
Subject: Re: Lisp Beginner looking for some help with creating a function.
Date: Sat, 12 Nov 2022 21:00:48 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <g4iN+w0SDq7=VQgmx@bongo-ra.co>
References: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 12 Nov 2022 21:00:48 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="d483190e17ec1fafcf391a828cd64a72";
logging-data="1316502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9rNPsCK2eME9oMnEgp/Ro"
Cancel-Lock: sha1:M8Zhij+X0RHzXgseIHZSmNUm1jk=
In-Reply-To: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Sat, 12 Nov 2022 21:00 UTC

On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
ME Jones <markjones@shawday.com> wrote:
> Hello,
> I am trying to write a function that returns the largest atom in a list of
> any depth. I am very new to lisp and learning. I do not want to use the
> built-in max function and am trying to create a simple function which takes
> a list of integers and returns the largest one. Here is my code so far,
> any help is appreciated, thanks.
>
> ;return the largest value greater than 0 in a list at any level of lists of integers.
> (defun largest_atom(L)
> (cond ((null L) 0)
> ((atom(car L))(cons(car L)(largest_atom(cdr L))))
> (if (< a b)
> b
> a
> )
> )
> )

Your code does not create bindings for a and b and does not assign
a value to them. You can create bindings with

(defun largest_atom (L &aux a b) .... )

or

(defun largest_atom (L)
(let (a b) ...rest of the code goes here...
))

From then on you can assign to a the maximum value found in (car L)
like

(if (atom (car L)) (setq a (car L)) (setq a ...left as an exercise...))

and to b the maximum value found in (cdr L) .
Finally you do (if (< a b) b a) .

> ;test
> ;returns 0 if the list is empty.
> ;(largest_atom '())
> ;should return 0
>
> ;test
> ;(largest_atom '((1 2) 3 ( 2 3 (1 9))))
> ;should return 9

Re: Lisp Beginner looking for some help with creating a function.

<87bkpbygi3.fsf@bsb.me.uk>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17227&group=comp.lang.lisp#17227

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.lisp
Subject: Re: Lisp Beginner looking for some help with creating a function.
Date: Sat, 12 Nov 2022 22:06:44 +0000
Organization: A noiseless patient Spider
Lines: 97
Message-ID: <87bkpbygi3.fsf@bsb.me.uk>
References: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="c7c6b248f03d89c1a600b10fbdc469e4";
logging-data="1325545"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+75Zw7dBOVqeLAmJbTJKeLrbFZ47IQtw8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:mqHv9bFWR5bgYAcULumvMbS9kbs=
sha1:sg5vfFLmGEBnwqe8uKEAxnOb5JY=
X-BSB-Auth: 1.4b9a99e334fa1115d419.20221112220644GMT.87bkpbygi3.fsf@bsb.me.uk
 by: Ben Bacarisse - Sat, 12 Nov 2022 22:06 UTC

ME Jones <markjones@shawday.com> writes:

> Hello,
> I am trying to write a function that returns the largest atom in a
> list of any depth. I am very new to lisp and learning.
> I do not want to use the built-in max function

Why? If I had been told not to use max, I'd define a basic 'my-max'
function and use that, since I think the clearest solution uses such a
function. Good Lisp style uses of lots of reusable functions and few
variables.

> and am trying to create a simple function which takes a list of
> integers and returns the largest one. Here is my code so far, any
> help is appreciated, thanks.
>
> ;return the largest value greater than 0 in a list at any level of
> lists of integers.

The test for atoms will include floating-point numbers, rationals,
characters, strings and so on. Of course > is not defined for most of
these, but you are certainly not limited to integers.

> (defun largest_atom(L)
> (cond ((null L) 0)
> ((atom(car L))(cons(car L)(largest_atom(cdr L))))
> (if (< a b)
> b
> a
> )
> )
> )

I would aim to do this without the extra variables a and b, but then I'd
define a max function if prohibited from using max!

And I would also simplify the specification so that

(largest_atom 5) == 5

and to allow for any s-expression so that

(largest_atom '(8 . 9)) == 9

This will allow for a very natural recursive implementation that, if
this is from a tutorial, is likely to be what is wanted. At the very
least, write it using max and then think how you would like to avoid
max.

Spoiler alert -- code outline below. Don't scroll until you've had a
go!

..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..

(defun largest_atom (l)
(cond ((null l) 0)
((atom l) l)
(t (max <what goes here> <and what goes here?))))

--
Ben.

Re: Lisp Beginner looking for some help with creating a function.

<s8QKi=2Zk=8EimizO@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17228&group=comp.lang.lisp#17228

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!aioe.org!wMjcvFyyQbKkD1DyxkS8fQ.user.46.165.242.91.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.lisp
Subject: Re: Lisp Beginner looking for some help with creating a function.
Date: Sun, 13 Nov 2022 11:46:30 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <s8QKi=2Zk=8EimizO@bongo-ra.co>
References: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="32704"; posting-host="wMjcvFyyQbKkD1DyxkS8fQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
X-Notice: Filtered by postfilter v. 0.9.2
 by: Spiros Bousbouras - Sun, 13 Nov 2022 11:46 UTC

On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
ME Jones <markjones@shawday.com> wrote:
> Hello,
> I am trying to write a function that returns the largest atom in a list of
> any depth. I am very new to lisp and learning. I do not want to use the
> built-in max function and am trying to create a simple function which takes
> a list of integers and returns the largest one. Here is my code so far,
> any help is appreciated, thanks.
>
> ;return the largest value greater than 0 in a list at any level of lists of integers.
> (defun largest_atom(L)
> (cond ((null L) 0)
> ((atom(car L))(cons(car L)(largest_atom(cdr L))))
> (if (< a b)
> b
> a
> )
> )
> )
> ;test
> ;returns 0 if the list is empty.
> ;(largest_atom '())
> ;should return 0
>
> ;test
> ;(largest_atom '((1 2) 3 ( 2 3 (1 9))))
> ;should return 9

When you have an implementation , you should test it when all the numbers
in the list are negative.

Re: Lisp Beginner looking for some help with creating a function.

<87r0y7vvmf.fsf@bsb.me.uk>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17229&group=comp.lang.lisp#17229

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.lisp
Subject: Re: Lisp Beginner looking for some help with creating a function.
Date: Sun, 13 Nov 2022 13:20:40 +0000
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87r0y7vvmf.fsf@bsb.me.uk>
References: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com>
<s8QKi=2Zk=8EimizO@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="210141583cb693d4c1628c934ef3c664";
logging-data="1546792"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19b7iqGGDw2AcJhNMRmxMV3rvJBExI2urM="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:TMNx49ruZEJOeuGmZUujaqXZYMI=
sha1:Dv93R08Ndp+yay0LtSlm0bqsEwY=
X-BSB-Auth: 1.93e018ddafed94023926.20221113132040GMT.87r0y7vvmf.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 13 Nov 2022 13:20 UTC

Spiros Bousbouras <spibou@gmail.com> writes:

> On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
> ME Jones <markjones@shawday.com> wrote:
<cut>
>> ;return the largest value greater than 0 in a list at any level of
>> lists of integers.
<cut>
> When you have an implementation , you should test it when all the numbers
> in the list are negative.

The "spec" appears to rule that out. I suspect that's because it makes
for a simple solution. That's why I thought this might be from some
online tutorial.

--
Ben.

Re: Lisp Beginner looking for some help with creating a function.

<danLfaOCMFQ+5drLH@bongo-ra.co>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17230&group=comp.lang.lisp#17230

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: spibou@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.lisp
Subject: Re: Lisp Beginner looking for some help with creating a function.
Date: Sun, 13 Nov 2022 14:05:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <danLfaOCMFQ+5drLH@bongo-ra.co>
References: <cb3bd708-a3e9-4b65-80ab-3d5edf4a41b9n@googlegroups.com> <s8QKi=2Zk=8EimizO@bongo-ra.co> <87r0y7vvmf.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 13 Nov 2022 14:05:39 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="50d251600a88d5dd1cd194954607d11e";
logging-data="1554391"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/TnnXzQsPacNh5igrSCdQx"
Cancel-Lock: sha1:hkX0vF9zi7sPFAHHM2nBHIES1dQ=
X-Server-Commands: nowebcancel
In-Reply-To: <87r0y7vvmf.fsf@bsb.me.uk>
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Sun, 13 Nov 2022 14:05 UTC

On Sun, 13 Nov 2022 13:20:40 +0000
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Spiros Bousbouras <spibou@gmail.com> writes:
>
> > On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
> > ME Jones <markjones@shawday.com> wrote:
> <cut>
> >> ;return the largest value greater than 0 in a list at any level of
> >> lists of integers.
> <cut>
> > When you have an implementation , you should test it when all the numbers
> > in the list are negative.
>
> The "spec" appears to rule that out. I suspect that's because it makes
> for a simple solution. That's why I thought this might be from some
> online tutorial.

It's not totally clear to me from the quote but perhaps that is the intended
meaning.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor