Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lisp Beginner looking for some help with creating a function.

39 views
Skip to first unread message

ME Jones

unread,
Nov 12, 2022, 2:42:40 PM11/12/22
to
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

Spiros Bousbouras

unread,
Nov 12, 2022, 4:00:53 PM11/12/22
to
On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
ME Jones <mark...@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) .

Ben Bacarisse

unread,
Nov 12, 2022, 5:06:51 PM11/12/22
to
ME Jones <mark...@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.

Spiros Bousbouras

unread,
Nov 13, 2022, 6:46:37 AM11/13/22
to
On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
ME Jones <mark...@shawday.com> wrote:
When you have an implementation , you should test it when all the numbers
in the list are negative.

Ben Bacarisse

unread,
Nov 13, 2022, 8:20:45 AM11/13/22
to
Spiros Bousbouras <spi...@gmail.com> writes:

> On Sat, 12 Nov 2022 11:42:37 -0800 (PST)
> ME Jones <mark...@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.

Spiros Bousbouras

unread,
Nov 13, 2022, 9:05:44 AM11/13/22
to
It's not totally clear to me from the quote but perhaps that is the intended
meaning.
0 new messages