Jump to content
  • Advertisement
Sign in to follow this  
sspeedy

beginner Scheme/Lisp learner :)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by sspeedy
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


That's a homework problem, not a question.

Share this post


Link to post
Share on other sites
Quote:
Original post by Russell
That'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.

Then ask us. after showing your current attempt at the solution.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

(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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!