[racket] Find nth term of a List

From: Stephen Bloch (bloch at adelphi.edu)
Date: Tue Sep 25 23:49:17 EDT 2012

On Sep 25, 2012, at 9:08 PM, Ashley Fowler wrote:

> so far I have 
> (define getNth (lambda (N LS)
>                    (if (eq? N 0)(car LS)
> which only includes the "base case".

And even with only the base case written, there's already a bug that will cause it to crash horribly.

> I need help on how to create this function. Any ideas?

No, you don't need help on how to create THIS function; you need help on how to create functions.  You've got several test cases with correct answers, which is one important step in the right direction (although I would also add a test case with an empty list).  The next (or perhaps previous!) step should be to really understand the data types of your inputs: a whole number and a list.  Both of these are recursively-defined data types, so you need to look at http://picturingprograms.com/download/chap25.pdf , which is entitled "Multiple recursive data".  Of course, that chapter assumes that you're already fluent at writing functions that operate on ONE recursive data type, e.g. http://picturingprograms.com/download/chap22.pdf and http://picturingprograms.com/download/chap24.pdf .

Stephen Bloch
sbloch at adelphi.edu

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120925/9d6d800e/attachment.html>

Posted on the users mailing list.