Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,583 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,937 people online right now. Registration is fast and FREE... Join Now!




Scheme: cons - misunderstanding recursive list building

 
Reply to this topicStart new topic

Scheme: cons - misunderstanding recursive list building

LowWaterMark
27 Sep, 2008 - 11:25 PM
Post #1

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
I'm struggling with something. Let me start with code for the rember function then ask. I'm asked to find the value of (rember a lat).
CODE
(define rember
   (lambda (a lat)
       (cond
          ((null? lat) (quote ()))
          ((eq? (car lat) a) (cdr lat))
          (else (cons (car lat)
                     (rember a (cdr lat)))))))

where argument:
a is and
and
lat is (bacon lettuce and tomato)

I'm working from "The Little Schemer" by Friedman and Felleisen. I understand that the function rember is checking each first atom (car) of the list of atoms (lat) in sequence, looking for identity with the argument a. Failing that, it squirrels away these non-a atoms to be constructed (cons) back onto the remaining lat once a is found and dispensed with (rember).

Here's my confusion and it has to do with the recursive behaviour in rebuilding my list once deconstructed.
1. By the time the function rember finds a, lat equals (rember a (cdr lat)) equals tomato.
2. Affixing car to the front of it with cons redefines lat to equal (lettuce tomato).
3. Where is bacon? Where does my function rember find it and cons it to lat?
**3a. As (cdr lat) redefined lat from (bacon lettuce and tomato) to(lettuce and tomato) to (and tomato), car was simultaneously redefined from bacon to lettuce.

In a nutshell: what happened to bacon?

thx

This post has been edited by LowWaterMark: 28 Sep, 2008 - 09:53 PM
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Scheme: Cons - Misunderstanding Recursive List Building
28 Sep, 2008 - 11:05 PM
Post #2

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
OK, Scheme uses too many parenthesis, recursion is intuitively self-evident and my question is boring. Nevertheless, I'm on day-two of trying to grasp this and it is driving me toward madness. If nobody responds my blood will be on your hands. Y'all will be like a bunch of Lady Macbeths messing up your keyboards.

Here's an extended version of the Scheme code withe the function's structure coinciding more closely with the structure of the argument (so they say).

CODE
(define rember
   (lambda (a lat)
      (cond
         ((null? lat) (quote ()))
         (else (cond
                    ((eq? (car lat) a) (cdr lat))
                    (else (cons (car lat)
                                (rember a
                                   (cdr lat)))))))))


lambda - initial two arguments:
a is and
lat is (bacon lettuce and tomato)

I'm sitting here with a penny, nickel, dime and quarter representing the atom a and the four atoms of my list of atoms lat (i.e. lambda's two arguments).

My question is the same. I don't understand what variable expression is holding the atom bacon by the time a - and - is peeled off of cdr lat by (rember a (cdr lat)).

Thanks for reading.
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Scheme: Cons - Misunderstanding Recursive List Building
29 Sep, 2008 - 12:30 AM
Post #3

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
Part of my confusion is that these recursive definitions seem to employ circular reasoning in their execution. If I backtrack in my text "The Little Schemer" to the definition of lat (i.e. list of atoms), I find:

CODE
(define lat
   (lambda (l)
      (cond
         ((null? l) #t)
         ((atom? (car l)) (lat? (cdr l)))
         (else #f))))


It asks if (cdr l) is a lat within its definition of lat. This seems to be a logical fallacy, specifically a fallacy called a circular definition. A circular definition is a specific case of failure to elucidate in logical fallacy. An argument is advanced where conclusion is implied or already assumed in the premise.

What am I missing here? Does (lat? (cdr l)) replace the value of l with (cdr l)? I know I'm mistaken as it's unlikely that I'm the first human to notice this.

This post has been edited by LowWaterMark: 29 Sep, 2008 - 01:01 AM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Scheme: Cons - Misunderstanding Recursive List Building
30 Sep, 2008 - 10:58 PM
Post #4

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(LowWaterMark @ 29 Sep, 2008 - 09:30 AM) *

Part of my confusion is that these recursive definitions seem to employ circular reasoning in their execution. If I backtrack in my text "The Little Schemer" to the definition of lat (i.e. list of atoms), I find:

CODE
(define lat
   (lambda (l)
      (cond
         ((null? l) #t)
         ((atom? (car l)) (lat? (cdr l)))
         (else #f))))


It asks if (cdr l) is a lat within its definition of lat. This seems to be a logical fallacy, specifically a fallacy called a circular definition. A circular definition is a specific case of failure to elucidate in logical fallacy. An argument is advanced where conclusion is implied or already assumed in the premise.

What am I missing here? Does (lat? (cdr l)) replace the value of l with (cdr l)? I know I'm mistaken as it's unlikely that I'm the first human to notice this.


May I suggest these Berkeley Lectures
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Scheme: Cons - Misunderstanding Recursive List Building
30 Sep, 2008 - 11:49 PM
Post #5

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
Thanks for taking the time to reply.

I now have the Berkeley courses bookmarked alongside those of MIT, Ars Technica, and Stanford. Is it safe to say that you like the quality of the Berkeley lectures?

I have the first one starting in my ear (and screen). I'm guessing by the title that it uses the Wizard Book by Sussman, et. al. I'm also guessing that my question either:
1. has no simple answer, or
2. reflects such a broad misunderstanding that you felt compelled to recommend an entire curriculum.

Either way, thank you.
User is offlineProfile CardPM
+Quote Post

LowWaterMark
RE: Scheme: Cons - Misunderstanding Recursive List Building
1 Oct, 2008 - 01:22 AM
Post #6

D.I.C Head
**

Joined: 30 Jul, 2008
Posts: 119



Thanked: 1 times
My Contributions
AdamSpeight2008 - thanks again for the link.

I just finished the first lecture and it is good. Hell, it's Berkeley. I see that there is a lecture dedicated to recursive and iterative programming, but that I fear is not going to answer my question.

My question is a nuts and bolts question. Please refer to my list of atoms, i.e. lat (bacon lettuce and tomato) question up top. During the second recursive "pass" for lack of a better word, what function or procedure or variable is holding the atom "bacon". As the lat is being stripped down, the function is searching for the member to remove. In other words rember is executing and I'm piling up atoms. First is bacon. Second is lettuce. Low and behold, and shows up and I need to get rid of it and start rebuilding, i.e. cons my lat to get the final value, (bacon lettuce tomato).

What is holding bacon while rember is consing lettuce onto tomato on its way back out of the recursion?

This post has been edited by LowWaterMark: 1 Oct, 2008 - 01:27 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:44AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month