# beginner Scheme/Lisp learner :)

This topic is 5198 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey guys, i'll be learning Scheme/Lisp this semester but i can already tell i'll need help with this stuff. the teacher has been going over 'car', 'cudr', and 'cons' and using them with lists in DrScheme. I'm not sure if I even understand the definitions, so while I'm also looking it up on the book, it'd be nice to learn from you guys in here (though it seems I may not get replies; seems heavy on C++ here!). if i get a few replies, i may post a couple of problems that the teacher gave us permission to practice/share with others. i'm sure i'll need help ;) anyways, let the fun begin.

##### Share on other sites
Um. You didn't post a question...

So, hi. What don't you get?

##### Share on other sites
ah sorry. i had wanted definitions of cdr, cons, and car but i just looked 'em up.
ok i'll post a question here:
Write the simple recursive procedure 'count' that returns the number of elements in a list.
> (count (list 2 2 2 2)
4

##### Share on other sites
Quote:
 Original post by sspeedyah sorry. i had wanted definitions of cdr, cons, and car but i just looked 'em up.ok i'll post a question here:Write the simple recursive procedure 'count' that returns the number of elements in a list.> (count (list 2 2 2 2)4

That's a homework problem, not a question.

##### Share on other sites
Quote:
 Original post by RussellThat's a homework problem, not a question.

Yeah. Just in case this isn't clear. Try to answer the question yourself, and then see what you can't figure out.

##### Share on other sites
(define count  (lambda (lst kollect)    (if (null? lst)        (kollect 0)        (count (cdr lst) (lambda (n) (kollect (+ 1 n)))))))

If he turns that in his teacher will stab him.

##### Share on other sites
gee thanks anonymous poster. i'd love to be stabbed--Not.
anyways, didn't mean to come off as I just want an answer (I realize I won't be learning crap).

the book talks about Fibonacci numbers and defines it into a recursive procedure (for computing Fibonacci numbers). i guess what's disturbing me (and i know this sounds stupid) but the def. of (fib n) calls itself in its own definition
(+ (fib(-n 1)) (fib (-n 2))). how does it know what to do if in its own definition it calls itself?

and for the count thing, i just keep staring at it. i don't know where to begin. any hints would be nice. the only thing i have down is:
(define (count alist)
(if (null? alist)
;i know here that alist is empty, so count should be 0

i don't know how to state that comment, or how i would call the function recursively to keep checking for numbers in the list and to count 'em.

beginner blues. hope my questions make sense. please keep in mind that i just started out.

##### Share on other sites
What is the count of an empty list? Suppose you have count-k which will return the count for lists with size less than or equal to k. How would you use count-k to complete the definition of count? That is, if given a function to compute the length of a k-length list, how do you compute the length of a k+1-length list?

You have two problems: 1) solve the easy case, 2) solve the induction case.

##### Share on other sites
You have the part about the count of '() being 0 correct, now try to express the count of a non-empty list in the count of the cdr (all except the first element) of that list.

About functions calling themselves, don't worry too much about it, it works. The identifiers you use inside functions are not actually used until the function is called, so it can safely call itself - by the time it executes the define statement will have finished and thus the function name will be correctly bound to the function.

##### Share on other sites
I take it you know about htdp.org

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013208
×