having trouble iterating over a list

Discussion of Scheme and Racket

having trouble iterating over a list

Postby kevinlisp » Mon Sep 07, 2009 12:25 pm

I am making a program that returns the respective factorials of a list of numbers. i need to make this without using the map keyword and im having lots of trouble finding a recursive way of finding the solution. I am not to sure how to make the recursive call reachable. The function factorial just returns the factorial of a number. L2 is the list of factorials that is being constructed.
Here is the code:
Code: Select all
(define (facs2 L L2)
    ((>= (length L) 1 )(facs2Help (cdr L) (car L) L2))
    (true(facs2 (cdr L) L2))
 (define (facs2Help L n L2)
   (cons(factorial n) L2)


Re: having trouble iterating over a list

Postby Unne » Tue Sep 08, 2009 12:59 pm

This sounds like homework; if so you should identify it as such. I'm unclear what you want your function to return, but it sounds like you want to take a list of numbers, and return a list of factorials. So you need one function that takes a list of numbers as input.

* If the list is not empty, return a cons of the factorial of the first item in the list, and the result of recursing on the rest of the list.
* If the list is empty, return an empty list.

Also please note that good Lisp style is not to put your closing parens on their own lines and usually to put a space between items in a list. Lisp usually also forgoes CamelCase in favor of dashed-words. e.g. your code could look like:

Code: Select all
(define (facs2 L L2)
    ((>= (length L) 1 ) (facs2-help (cdr L) (car L) L2))
    (true (facs2 (cdr L) L2))))

(define (facs2-help L n L2)
  (cons (factorial n) L2))
Posts: 32
Joined: Sat Jun 28, 2008 6:10 pm
Location: Oregon

Return to Scheme

Who is online

Users browsing this forum: No registered users and 1 guest