Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

God made machine language; all the rest is the work of man.


devel / comp.lang.lisp / Can someone help me fix a basic recursive function

SubjectAuthor
* Can someone help me fix a basic recursive functionME Jones
+* Re: Can someone help me fix a basic recursive functionST
|`- Re: Can someone help me fix a basic recursive functionSpiros Bousbouras
+* Re: Can someone help me fix a basic recursive functionPaul Rubin
|+* Re: Can someone help me fix a basic recursive functionSpiros Bousbouras
||`* Re: Can someone help me fix a basic recursive functionPaul Rubin
|| `- Re: Can someone help me fix a basic recursive functionSpiros Bousbouras
|`* Re: Can someone help me fix a basic recursive functionBen Bacarisse
| `- Re: Can someone help me fix a basic recursive functionPaul Rubin
+* Re: Can someone help me fix a basic recursive functionSpiros Bousbouras
|`- Re: Can someone help me fix a basic recursive functionSpiros Bousbouras
+- Re: Can someone help me fix a basic recursive functionTom Russ
`* Re: Can someone help me fix a basic recursive functionME Jones
 +- Re: Can someone help me fix a basic recursive functionAxel Reichert
 +- Re: Can someone help me fix a basic recursive functionST
 `- Re: Can someone help me fix a basic recursive functionSpiros Bousbouras

1
Can someone help me fix a basic recursive function

<d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:a0c:ecc8:0:b0:4c6:91f9:9dff with SMTP id o8-20020a0cecc8000000b004c691f99dffmr3776191qvq.103.1669091528574;
Mon, 21 Nov 2022 20:32:08 -0800 (PST)
X-Received: by 2002:a05:6830:2b2b:b0:66c:52d6:50c8 with SMTP id
l43-20020a0568302b2b00b0066c52d650c8mr11583714otv.200.1669091528336; Mon, 21
Nov 2022 20:32:08 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Mon, 21 Nov 2022 20:32:08 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=67.185.73.248; posting-account=z6W7sQoAAADU1WINpl0JBaSWNNUw5WQ0
NNTP-Posting-Host: 67.185.73.248
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
Subject: Can someone help me fix a basic recursive function
From: markjones@shawday.com (ME Jones)
Injection-Date: Tue, 22 Nov 2022 04:32:08 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1456
 by: ME Jones - Tue, 22 Nov 2022 04:32 UTC

Hello,
I am trying to create the following recursive function:

Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)

(defun doubleodd (list)
(when list
(if (oddp (car list)) (setf (car list) (* (car list) 2)))
(doubleodd (cdr list))
)
)

When i evaluate i am not getting back a list and it's missing some values in the list. What am i doing wrong?

(doubleodd '(1 2 3 4 5))

evaluates to:
26410
NIL

Thank you for your help.
Mark

Re: Can someone help me fix a basic recursive function

<tlho6p$1i0r$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: usenet.233ph@slmail.me (ST)
Newsgroups: comp.lang.lisp
Subject: Re: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 05:54:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <tlho6p$1i0r$1@dont-email.me>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
Reply-To: usenet.233ph@slmail.me
Injection-Date: Tue, 22 Nov 2022 05:54:33 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="8b16de5e26c04878815b3f92ec50420f";
logging-data="51227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6bHHwzmYjREILLWsq86aF9hMko7LksK4="
User-Agent: slrn/1.0.3 (Darwin)
Cancel-Lock: sha1:JVh7yVDp5GovtUiT5dX/i8SVk50=
 by: ST - Tue, 22 Nov 2022 05:54 UTC

On 2022-11-22, ME Jones <markjones@shawday.com> wrote:
> Hello,
> I am trying to create the following recursive function:
>
> Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
>
> (defun doubleodd (list)
> (when list
> (if (oddp (car list)) (setf (car list) (* (car list) 2)))
> (doubleodd (cdr list))
> )
> )
>
> When i evaluate i am not getting back a list and it's missing some values in the list. What am i doing wrong?

(defun doubleodd (l)
(if (null l)
nil
(if (oddp (car l))
(cons (* 2 (car l)) (doubleodd (cdr l)))
(cons (car l) (doubleodd (cdr l))))))

In a recursive function with a list, 2 important things to remember:

you apply the function on the car of the list then your recursive call
goes on the cdr.

Once the list is null, you return a nil to end the recursion.

Lisp is mostly a functional language, avoid the usage of (setf) inside
a function unless it's to change the value of a lexical
variable. Otherwise, you function is called destructive.

2 books to read absolutely to understand Lisp and its philosophy:

Ansi Common Lisp
On Lisp

Both from Paul Graham. The second one is not printed anymore, but
available at low cost from lulu.com

>
> (doubleodd '(1 2 3 4 5))
>
> evaluates to:
> 26410
> NIL
>
> Thank you for your help.
> Mark

Re: Can someone help me fix a basic recursive function

<8735ab1iqf.fsf@nightsong.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.lisp
Subject: Re: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 00:44:24 -0800
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <8735ab1iqf.fsf@nightsong.com>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="81d9dc1e7acd10ee3590cb89caced2c3";
logging-data="72442"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OY01/Uthok3kn3GEXFwb8"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:6Ph251AFp6hG0pZKmcJOuMmowQU=
sha1:y6uKLFY11ltURDM/+hF+4JWaHU4=
 by: Paul Rubin - Tue, 22 Nov 2022 08:44 UTC

ME Jones <markjones@shawday.com> writes:
> I am trying to create the following recursive function:
> Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)

I would first break this out into a function that conditionally doubles
a single number:

(defun double-odd-1 (n)
(cond ((oddp n) (+ n n))
(t n)))

and then a tail-recursive function that applies DOUBLE-ODD-1 to each
element of a list, consing it up into a new list that is in reverse
order. Then, at the end, reverse the new list "in place" using
nreverse:

(defun double-odd (xs &optional acc)
(defun f (n) (+ n n))
(cond ((null xs) (nreverse acc))
(t (double-odd (cdr xs) (cons (double-odd-1 (car xs)) acc)))))

Using an accumulation parameter allows the recursive call to be the
function's final value, which means the compiler can actually turn it
into a simple jump rather than pushing one level deeper into a stack on
every call. That is kind of a lame explanation, but it is a very
important idiom in functional programming, so you should probably spend
some time studying it to understand it, e.g. by doing a web search on
"tail recursion" and looking at explanations in the results.

Pure functional style would also prefer using reverse instead of
nreverse, but using nreverse this way is a Common Lisp idiom.

Finally, notice that the pattern of creating a new list by applying some
function to every element of an old list is very common, and CL (and
related languages) usually have a built-in for it, called some variant
of "map". In CL's case, that function is called MAPCAR, so you would
simply write:

(defun double-odd (xs)
(mapcar #'double-odd-1 xs))

where DOUBLE-ODD-1 is as defined above.

Re: Can someone help me fix a basic recursive function

<h4eUsC6VK7qVIoHqv@bongo-ra.co>

  copy mid

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

  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: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 12:46:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <h4eUsC6VK7qVIoHqv@bongo-ra.co>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 22 Nov 2022 12:46:16 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3ab888a9ecc63ea627a0e8efd3051d1c";
logging-data="113772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5qHq3AcyVDL9veG81I/21"
Cancel-Lock: sha1:ZKbYs2HJ2LmvY1CTqr0Ly+dIDj8=
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
In-Reply-To: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
 by: Spiros Bousbouras - Tue, 22 Nov 2022 12:46 UTC

On Mon, 21 Nov 2022 20:32:08 -0800 (PST)
ME Jones <markjones@shawday.com> wrote:
> Hello,
> I am trying to create the following recursive function:
>
> Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
>
> (defun doubleodd (list)
> (when list
> (if (oddp (car list)) (setf (car list) (* (car list) 2)))
> (doubleodd (cdr list))
> )
> )
>
> When i evaluate i am not getting back a list and it's missing some values
> in the list. What am i doing wrong?
>
> (doubleodd '(1 2 3 4 5))
>
> evaluates to:
> 26410
> NIL

Others have explained what you're doing wrong but I'm wondering in which
implementation (doubleodd '(1 2 3 4 5)) returns 26410 .I tried clisp
and SBCL and the code just returns NIL which is what I would have expected.
No matter what happens earlier , the recursion will terminate by calling
doubleodd with argument NIL .Try the following modified version and study
its output.

(defun doubleodd2 (list &aux ret)
(format t "Received ~S~%" list)
(when list
(if (oddp (car list)) (setf (car list) (* (car list) 2)))
(setq ret (doubleodd (cdr list)))
(format t "Got back ~S~%" ret)
ret))
--
And my personal favourite - carries out > 400 circumcisions per week. I
think he uses the foreskins in his Fortune 500 company making wallets
that expand into suitcases when rubbed.
http://www.freeratio.org/showthread.php?p=7302620#post7302620

Re: Can someone help me fix a basic recursive function

<EKon0d876u8XJI=2W@bongo-ra.co>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.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: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 12:52:28 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <EKon0d876u8XJI=2W@bongo-ra.co>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com> <8735ab1iqf.fsf@nightsong.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="24715"; posting-host="wMjcvFyyQbKkD1DyxkS8fQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
X-Organisation: Weyland-Yutani
X-Server-Commands: nowebcancel
 by: Spiros Bousbouras - Tue, 22 Nov 2022 12:52 UTC

On Tue, 22 Nov 2022 00:44:24 -0800
Paul Rubin <no.email@nospam.invalid> wrote:
> ME Jones <markjones@shawday.com> writes:
> > I am trying to create the following recursive function:
> > Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
>
> I would first break this out into a function that conditionally doubles
> a single number:
>
> (defun double-odd-1 (n)
> (cond ((oddp n) (+ n n))
> (t n)))
>
> and then a tail-recursive function that applies DOUBLE-ODD-1 to each
> element of a list, consing it up into a new list that is in reverse
> order. Then, at the end, reverse the new list "in place" using
> nreverse:
>
> (defun double-odd (xs &optional acc)
> (defun f (n) (+ n n))
What's this for ?

> (cond ((null xs) (nreverse acc))
> (t (double-odd (cdr xs) (cons (double-odd-1 (car xs)) acc)))))
>
> Using an accumulation parameter allows the recursive call to be the
> function's final value, which means the compiler can actually turn it
> into a simple jump rather than pushing one level deeper into a stack on
> every call. That is kind of a lame explanation, but it is a very
> important idiom in functional programming, so you should probably spend
> some time studying it to understand it, e.g. by doing a web search on
> "tail recursion" and looking at explanations in the results.

Re: Can someone help me fix a basic recursive function

<coQdJ6CGVXBEFpxsy@bongo-ra.co>

  copy mid

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

  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: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 13:05:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <coQdJ6CGVXBEFpxsy@bongo-ra.co>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com> <tlho6p$1i0r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 22 Nov 2022 13:05:34 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3ab888a9ecc63ea627a0e8efd3051d1c";
logging-data="116766"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zyTS5VeIBT5czZvtX6cp8"
Cancel-Lock: sha1:/YnF+uLRnPdQyzyAKUTIcbqSumw=
X-Server-Commands: nowebcancel
In-Reply-To: <tlho6p$1i0r$1@dont-email.me>
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Tue, 22 Nov 2022 13:05 UTC

On Tue, 22 Nov 2022 05:54:33 -0000 (UTC)
ST <usenet.233ph@slmail.me> wrote:
> On 2022-11-22, ME Jones <markjones@shawday.com> wrote:
> > Hello,
> > I am trying to create the following recursive function:
> >
> > Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
> >
> > (defun doubleodd (list)
> > (when list
> > (if (oddp (car list)) (setf (car list) (* (car list) 2)))
> > (doubleodd (cdr list))
> > )
> > )
> >
> > When i evaluate i am not getting back a list and it's missing some values in the list. What am i doing wrong?
>
>
> (defun doubleodd (l)
> (if (null l)
> nil
> (if (oddp (car l))
> (cons (* 2 (car l)) (doubleodd (cdr l)))
> (cons (car l) (doubleodd (cdr l))))))
>
> In a recursive function with a list, 2 important things to remember:
>
> you apply the function on the car of the list then your recursive call
> goes on the cdr.
>
> Once the list is null, you return a nil to end the recursion.
>
> Lisp is mostly a functional language, avoid the usage of (setf) inside
> a function unless it's to change the value of a lexical
> variable. Otherwise, you function is called destructive.

Common Lisp is a multi-paradigm language and it is up to individual preference
when one should use SETF .But SETF is specified to return the new value assigned
so that is another reason the general idea the opening poster had would not
work. For example

* (let ((l (list 1 2)))
(setf (car l) (* 2 (car l))))
2

* (let ((l (list 1 2)))
(setf (car l) (* 2 (car l)))
l)

(2 2)

> 2 books to read absolutely to understand Lisp and its philosophy:
>
> Ansi Common Lisp
> On Lisp
>
> Both from Paul Graham. The second one is not printed anymore, but
> available at low cost from lulu.com

Personally I don't believe in language "philosophies" , the concept is
too vague and subjective. I'll just note that "On Lisp" also exists at
http://www.paulgraham.com/onlisptext.html .

Re: Can someone help me fix a basic recursive function

<8735abkpcd.fsf@bsb.me.uk>

  copy mid

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

  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: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 14:58:58 +0000
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <8735abkpcd.fsf@bsb.me.uk>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
<8735ab1iqf.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="9b85f22d308ce2c1806d1ce4165d5a1d";
logging-data="131121"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+IPbeD1V6C1jbgIh0HVsB4sY5ShEoTetY="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:vwMExinF4PVlUId5wXJq4GDktf0=
sha1:fGOaRNByZBvHaFHrM7dE9WU2GoY=
X-BSB-Auth: 1.ba30087933527ae8fcfc.20221122145858GMT.8735abkpcd.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 22 Nov 2022 14:58 UTC

Paul Rubin <no.email@nospam.invalid> writes:

> ME Jones <markjones@shawday.com> writes:
>> I am trying to create the following recursive function:
>> Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
>
> I would first break this out into a function that conditionally doubles
> a single number:
>
> (defun double-odd-1 (n)
> (cond ((oddp n) (+ n n))
> (t n)))

This is specific enough that I would make it a lambda. Are we going to
need it for anything other than this exercise?

> and then a tail-recursive function that applies DOUBLE-ODD-1 to each
> element of a list, consing it up into a new list that is in reverse
> order.

I don't see this as a use-case for an accumulating parameter. Some
tutorials will want the plain hand-written recursive solution, but
really the way to go is surely with mapcar as you suggest:

> Finally, notice that the pattern of creating a new list by applying some
> function to every element of an old list is very common, and CL (and
> related languages) usually have a built-in for it, called some variant
> of "map". In CL's case, that function is called MAPCAR, so you would
> simply write:
>
> (defun double-odd (xs)
> (mapcar #'double-odd-1 xs))
>
> where DOUBLE-ODD-1 is as defined above.

(defun doubleodd (list)
(mapcar (lambda (x) (if (oddp x) (* 2 x) x)) list))

--
Ben.

Re: Can someone help me fix a basic recursive function

<13c89cae-ff57-4ce6-a97b-740b584e1944n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:a05:622a:514d:b0:3a5:258c:d69c with SMTP id ew13-20020a05622a514d00b003a5258cd69cmr8654393qtb.279.1669135052472;
Tue, 22 Nov 2022 08:37:32 -0800 (PST)
X-Received: by 2002:a05:6808:1592:b0:35a:e1a7:c3b2 with SMTP id
t18-20020a056808159200b0035ae1a7c3b2mr2088013oiw.223.1669135052022; Tue, 22
Nov 2022 08:37:32 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Tue, 22 Nov 2022 08:37:31 -0800 (PST)
In-Reply-To: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2620:0:102f:1005:2671:984:2fee:34fe;
posting-account=05zmAwoAAAAJZM-3jv1hCWLHGZQceqwA
NNTP-Posting-Host: 2620:0:102f:1005:2671:984:2fee:34fe
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <13c89cae-ff57-4ce6-a97b-740b584e1944n@googlegroups.com>
Subject: Re: Can someone help me fix a basic recursive function
From: taruss@google.com (Tom Russ)
Injection-Date: Tue, 22 Nov 2022 16:37:32 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2682
 by: Tom Russ - Tue, 22 Nov 2022 16:37 UTC

On Monday, November 21, 2022 at 8:32:11 PM UTC-8, mark...@shawday.com wrote:
> Hello,
> I am trying to create the following recursive function:
>
> Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
>
> (defun doubleodd (list)
> (when list
> (if (oddp (car list)) (setf (car list) (* (car list) 2)))
> (doubleodd (cdr list))
> )
> )
>
> When i evaluate i am not getting back a list and it's missing some values in the list. What am i doing wrong?
>
> (doubleodd '(1 2 3 4 5))

There are a lot of other answers about how to update the function. I just want to
point out some other somewhat subtle issues with the code as presented here.

First off, the code uses (SETF (CAR LIST) ...) which will destructively mutate
the list. Generally as a beginner in the language you don't want to do destructive
list operations. It is really an optimization and can cause some very hard-to-debug
problems. So it is best to avoid it until you have a good understanding of lisp.

As an example of that, you are invoking your DOUBLEODD function with a constant
argument of '(1 2 3 4 5). And the code then destructively modifies that constant
list. This is undefined behavior in Common Lisp and must be avoided. You cannot
predict the results of doing this. There is no requirement that the compiler or in
this case the run time warn you about doing something like this. Common Lisp
allows you to do unwise things that might be prohibited in other languages.

Re: Can someone help me fix a basic recursive function

<1z+6RsOiYBxwpv9Qj@bongo-ra.co>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.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: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 19:33:21 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <1z+6RsOiYBxwpv9Qj@bongo-ra.co>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com> <h4eUsC6VK7qVIoHqv@bongo-ra.co>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="39124"; posting-host="wMjcvFyyQbKkD1DyxkS8fQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
X-Server-Commands: nowebcancel
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Tue, 22 Nov 2022 19:33 UTC

On Tue, 22 Nov 2022 12:46:16 -0000 (UTC)
Spiros Bousbouras <spibou@gmail.com> wrote:
> On Mon, 21 Nov 2022 20:32:08 -0800 (PST)
> ME Jones <markjones@shawday.com> wrote:
> > Hello,
> > I am trying to create the following recursive function:
> >
> > Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
> >
> > (defun doubleodd (list)
> > (when list
> > (if (oddp (car list)) (setf (car list) (* (car list) 2)))
> > (doubleodd (cdr list))
> > )
> > )
> >
> > When i evaluate i am not getting back a list and it's missing some values
> > in the list. What am i doing wrong?
> >
> > (doubleodd '(1 2 3 4 5))
> >
> > evaluates to:
> > 26410
> > NIL
>
> Others have explained what you're doing wrong but I'm wondering in which
> implementation (doubleodd '(1 2 3 4 5)) returns 26410 .I tried clisp
> and SBCL and the code just returns NIL which is what I would have expected.

As Tom Russ points out in <13c89cae-ff57-4ce6-a97b-740b584e1944n@googlegroups.com> ,
modifying a quoted object is undefined behaviour so it was wrong of me to expect
anything from (doubleodd '(1 2 3 4 5)) .But

(doubleodd (list 1 2 3 4 5))

should return NIL .

Re: Can someone help me fix a basic recursive function

<b6921928-1453-4e94-b6ef-3f3cab6dd4c1n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
X-Received: by 2002:a05:622a:110:b0:3a5:50fa:1a32 with SMTP id u16-20020a05622a011000b003a550fa1a32mr6517785qtw.11.1669148467226;
Tue, 22 Nov 2022 12:21:07 -0800 (PST)
X-Received: by 2002:a05:6870:9f8c:b0:13c:5763:9406 with SMTP id
xm12-20020a0568709f8c00b0013c57639406mr5542539oab.200.1669148467025; Tue, 22
Nov 2022 12:21:07 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.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: Tue, 22 Nov 2022 12:21:06 -0800 (PST)
In-Reply-To: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=67.185.73.248; posting-account=z6W7sQoAAADU1WINpl0JBaSWNNUw5WQ0
NNTP-Posting-Host: 67.185.73.248
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b6921928-1453-4e94-b6ef-3f3cab6dd4c1n@googlegroups.com>
Subject: Re: Can someone help me fix a basic recursive function
From: markjones@shawday.com (ME Jones)
Injection-Date: Tue, 22 Nov 2022 20:21:07 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4175
 by: ME Jones - Tue, 22 Nov 2022 20:21 UTC

On Monday, November 21, 2022 at 8:32:11 PM UTC-8, ME Jones wrote:
> Hello,
> I am trying to create the following recursive function:
>
> Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
>
> (defun doubleodd (list)
> (when list
> (if (oddp (car list)) (setf (car list) (* (car list) 2)))
> (doubleodd (cdr list))
> )
> )
>
> When i evaluate i am not getting back a list and it's missing some values in the list. What am i doing wrong?
>
> (doubleodd '(1 2 3 4 5))
>
> evaluates to:
> 26410
> NIL
>
> Thank you for your help.
> Mark

Thank you all, for the help, very much appreciated.

I am learning and for sure will be making mistakes and i really appreciate the feedback, coding help, and references to reading material.

Here are the four functions that I have been working on:

I am still uncertain with large_atom because I am trying to accomplish without using let or higher order functions such as mapcar, and also solving using recursion and not iterative.

Again, Thank You!

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The largest value greater than 0 in the list at any level of lists of integers.
;Example: (large_atom '((1 2) 3 ( 2 3 (1 9)))) -> 9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun large_atom (lst)
(if (and (null (cdr lst)) (not (listp (car lst)))) (car lst)
(if (listp (car lst))
(let (
(x (large_atom (car lst)))
(y (if (null (cdr lst)) '() (large_atom (cdr lst))))
)
(if (null y) x (if (> x y) x y))
)
(let (
(z (large_atom (cdr lst)))
)
(if (> (car lst) z) (car lst) z)
)
)
)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
a list where all the integers are bigger than the input integer.
;Example: (biggerthan 3 '(1 2 3 4 5)) -> ( 4 5 )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun biggerthan (val L)
(cond
((null L) nil)
((> (car L) val) (cons (car L)
(biggerthan val (cdr L))))
(t (biggerthan val (cdr L)))
)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;A list where all the odd numbers have been doubled.
;Example: (doubleodd '(1 2 3 4 5)) -> (2 2 6 4 10)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun doubleodd (l)
(if (null l) nil
(if (oddp (car l))
(cons (* 2 (car l)) (doubleodd (cdr l)))
(cons (car l) (doubleodd (cdr l)))
)
)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;The product of all the even integers in a list.
;Example: (evenprod '(1 2 3 4 5)) -> 8 ; because 2 * 4 = 8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun evenprod (l)
(if (null l) nil
(if (evenp (car l))
(* (car l) (evenprod (cdr l)))
(evenprod (cdr l))
)
)
)

Re: Can someone help me fix a basic recursive function

<87mt8i681e.fsf@axel-reichert.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: mail@axel-reichert.de (Axel Reichert)
Newsgroups: comp.lang.lisp
Subject: Re: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 21:36:29 +0100
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <87mt8i681e.fsf@axel-reichert.de>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
<b6921928-1453-4e94-b6ef-3f3cab6dd4c1n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="775db0bc1cf7f9917ee13b1fc17d1f48";
logging-data="186547"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bByjCAq3fIMt20sve4tpP3QZvR804oec="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:q/I9MJ+6O50tsEsE2GglR4zaf4I=
sha1:Gd0UZMk0za46FcU4iTVm7NtbJyc=
 by: Axel Reichert - Tue, 22 Nov 2022 20:36 UTC

ME Jones <markjones@shawday.com> writes:

> I am learning and for sure will be making mistakes and i really
> appreciate the feedback, coding help, and references to reading
> material.

When I ventured out into Common Lisp some years back, I found

https://www.cs.cmu.edu/~dst/LispBook/book.pdf

a very nice read, with lots of small recursive exercises.

Have fun! For me, learning a Lisp language (the whole family really
"clicked" wich me) was a great eye-opener, connecting an awful lot of
loose ends I encountered while using other languages and paradigms.

Best regards

Axel

Re: Can someone help me fix a basic recursive function

<87y1s2zjyv.fsf@nightsong.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.lisp
Subject: Re: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 14:46:00 -0800
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <87y1s2zjyv.fsf@nightsong.com>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
<8735ab1iqf.fsf@nightsong.com> <EKon0d876u8XJI=2W@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="c634b32df2db9661c71dcb80962e4b68";
logging-data="204701"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zv+MKeRhivfDYeaMWQdAF"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:e5mcKOJwSof1sSWq52yxmo88Y+k=
sha1:+uTwF/3Vnur/JtgZwXcyixuQ9lk=
 by: Paul Rubin - Tue, 22 Nov 2022 22:46 UTC

Spiros Bousbouras <spibou@gmail.com> writes:
>> (defun double-odd (xs &optional acc)
>> (defun f (n) (+ n n))
> What's this for ?

Woops, sorry, f is a leftover from testing. Just delete that line. I
first tried to define the helper function internally like one would do
in Scheme, but it didn't work. I think in CL you are supposed to do it
with FLET but I didn't bother.

Re: Can someone help me fix a basic recursive function

<87tu2qzijp.fsf@nightsong.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.lisp
Subject: Re: Can someone help me fix a basic recursive function
Date: Tue, 22 Nov 2022 15:16:42 -0800
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <87tu2qzijp.fsf@nightsong.com>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
<8735ab1iqf.fsf@nightsong.com> <8735abkpcd.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="c3cf9475f08ce528ba79902b270cff95";
logging-data="210296"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+a23NgSJ2fbEYhXvKWq054"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:rkVJ5+gJ/3s++IzSc6E1XmB6CLY=
sha1:CsH9bguXrduikyak9e7AqQCyPQg=
 by: Paul Rubin - Tue, 22 Nov 2022 23:16 UTC

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> This is specific enough that I would make it a lambda. Are we going to
> need it for anything other than this exercise?

It's just to be more understandable. Of course in real life I'd use
mapcar, or maybe FLET with a lambda. I didn't want to introduce (or
look up) more complicated syntax since this was a tutorial example.

> I don't see this as a use-case for an accumulating parameter.

The idea is to avoid recursing on the stack out to the length of the
list.

> Some tutorials will want the plain hand-written recursive solution,

True, and we saw some posts showing how to do that. It's usually
preferable to use tail recursion when you can, though, so I showed how
to do that.

> but really the way to go is surely with mapcar as you suggest: ...
> (defun doubleodd (list)
> (mapcar (lambda (x) (if (oddp x) (* 2 x) x)) list))

Yep.

Re: Can someone help me fix a basic recursive function

<tlk4ti$a4el$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: usenet.233ph@slmail.me (ST)
Newsgroups: comp.lang.lisp
Subject: Re: Can someone help me fix a basic recursive function
Date: Wed, 23 Nov 2022 03:43:46 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <tlk4ti$a4el$1@dont-email.me>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com>
<b6921928-1453-4e94-b6ef-3f3cab6dd4c1n@googlegroups.com>
Reply-To: usenet.233ph@slmail.me
Injection-Date: Wed, 23 Nov 2022 03:43:46 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="70554b950465ba74602c0acc7a3db563";
logging-data="332245"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+hDo9Zqs+W8HN5gdODsxtJZsVXPhV3jAE="
User-Agent: slrn/1.0.3 (Darwin)
Cancel-Lock: sha1:hkyTPS7ESCOyvQP6sv1P3OrgWaU=
 by: ST - Wed, 23 Nov 2022 03:43 UTC

On 2022-11-22, ME Jones <markjones@shawday.com> wrote:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> a list where all the integers are bigger than the input integer.
> ;Example: (biggerthan 3 '(1 2 3 4 5)) -> ( 4 5 )
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> (defun biggerthan (val L)
> (cond
> ((null L) nil)
> ((> (car L) val) (cons (car L)
> (biggerthan val (cdr L))))
> (t (biggerthan val (cdr L)))
> )
> )

Shorter, simpler, clearer:

(loop :for x :in '(1 2 3 4 5)
:when (> x 3) collect x)

Re: Can someone help me fix a basic recursive function

<WxiOd=xak7THrbTLT@bongo-ra.co>

  copy mid

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

  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: Can someone help me fix a basic recursive function
Date: Wed, 23 Nov 2022 04:49:43 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <WxiOd=xak7THrbTLT@bongo-ra.co>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com> <8735ab1iqf.fsf@nightsong.com> <EKon0d876u8XJI=2W@bongo-ra.co>
<87y1s2zjyv.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 23 Nov 2022 04:49:43 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="3421480630979a753d090bfc083d821d";
logging-data="343515"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9OBBDL2Z5w8mVL0jBiP37"
Cancel-Lock: sha1:6VRhz9jKcRS3Iv0SWr3E3TS4/xE=
X-Server-Commands: nowebcancel
In-Reply-To: <87y1s2zjyv.fsf@nightsong.com>
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Wed, 23 Nov 2022 04:49 UTC

On Tue, 22 Nov 2022 14:46:00 -0800
Paul Rubin <no.email@nospam.invalid> wrote:
> Spiros Bousbouras <spibou@gmail.com> writes:
> >> (defun double-odd (xs &optional acc)
> >> (defun f (n) (+ n n))
> > What's this for ?
>
> Woops, sorry, f is a leftover from testing. Just delete that line. I
> first tried to define the helper function internally like one would do
> in Scheme, but it didn't work. I think in CL you are supposed to do it
> with FLET but I didn't bother.

Yes , if you want to make it local , you use FLET or LABELS .

Re: Can someone help me fix a basic recursive function

<uBLXyxEjGviP=tQM5@bongo-ra.co>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.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: Can someone help me fix a basic recursive function
Date: Wed, 23 Nov 2022 05:32:12 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <uBLXyxEjGviP=tQM5@bongo-ra.co>
References: <d088e62b-7798-4d56-b8c2-5de08acc10e8n@googlegroups.com> <b6921928-1453-4e94-b6ef-3f3cab6dd4c1n@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="50278"; posting-host="wMjcvFyyQbKkD1DyxkS8fQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
X-Server-Commands: nowebcancel
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Wed, 23 Nov 2022 05:32 UTC

On Tue, 22 Nov 2022 12:21:06 -0800 (PST)
ME Jones <markjones@shawday.com> wrote:
> Thank you all, for the help, very much appreciated.
>
> I am learning and for sure will be making mistakes and i really appreciate
> the feedback, coding help, and references to reading material.
>
> Here are the four functions that I have been working on:
>
> I am still uncertain with large_atom because I am trying to accomplish
> without using let or higher order functions such as mapcar, and also
> solving using recursion and not iterative.

I'm not sure what you mean not using LET .What construct are you trying to
avoid ? Anyway , I didn't test your code but it looks correct to me with
1 exception :

[...]

> (defun evenprod (l)
> (if (null l) nil
> (if (evenp (car l))
> (* (car l) (evenprod (cdr l)))
> (evenprod (cdr l))
> )
> )
> )

Should be
if (null l) 1

--
vlaho.ninja/prog

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor