views:

106

answers:

2

Hi,

I'm trying to use scheme to write a function f that takes a number n and a function g and returns a list of lists of length n, but according with booleans according to the pattern indicated by g. For example, the function f should take n say 3 and the function g which makes every 3rd item on the list a true. It should return this:

(list (list true true false)
      (list true true false)
      (list true true false))

I have no idea where to start with this, so any help or tips would be greatly appreciated. THanks!

+1  A: 

Key to this looks like using map.

In short, map takes a function and a list, and applies the function to that list, returning the modified list.

To do this, as I'm assuming you don't want a direct solution, I'd first construct your list of lists (just use an accumulator -- count down -- to make the list as long as needed, and use cons and list and quote for everything else.

Then simply apply map (probably twice, depending on how you implement your solution) to the list of lists. (eg list l and function fn: (map fn (car l)) and recur with the cdr of l and cons the results back together).

Good luck! Hope this helps!

Isaac Hodes
+1  A: 

I don't quite follow what you're trying to do, but in addition to map, you might find build-list to be useful. build-list takes a number and a procedure, takes the range from 0 to that number less 1, and maps your procedure over that range. e.g.

> (build-list 5 (lambda (x) (* x x)))
(0 1 4 9 16)
mquander