CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Fluent UDF and Scheme Programming (
-   -   Need help in Scheme Programming - Mulltiplying list elements (

ydan87 November 8, 2011 18:17

Need help in Scheme Programming - Mulltiplying list elements
Hello everyone, I've ran into this site and was wondering if you can advise me something.
The program I'm trying to write called (multListsA lst1 lst2) which receives 2 lists and returns a new list in which the i'th element is the product of the two i'th elements from the 2 given lists. MOREOVER, in case that one list is shorter than the other, the shorter list should be used from the beginning again. Which means 2 things:
1) The result list should have the same length as the longer list.
2) I have to use the modulo option at some point.

I'm trying to write a recursive solution to that nice program, but I can't seem to find the right formula and the right use of modulo to solve this.

Can you please help me?
Thanks in advance

fox000002 November 8, 2011 23:55

What is "modulo option"?


(define (multiply la lb)
  (define n (max (length la) (length lb)))
  (let loop ((i 0) (l '()) (a la) (b lb))
    (if (>= i n)
        (if (null? a) (set! a la) #f)
        (if (null? b) (set! b lb) #f)               
        (set! l (append l (list (* (car a) (car b)))))
        (loop (+ i 1) l (cdr a) (cdr b))))))

ydan87 November 9, 2011 01:05


Originally Posted by fox000002 (Post 331301)
What is "modulo option"?

Modulo option (or remainder) is the way to solve this program recursively.
You're referring to the place in the list, dividing in the smaller list's length in order to stay within the list until the longer one is finished.

I know my explaination is lame, may be someone understood it anyway?

All times are GMT -4. The time now is 18:47.