# more scheme questions

## Recommended Posts

red-dragonX    122
hey all, thanks for help with last post. I would've just added the content of this post as the edit of the last, but wasn't sure if you guys would realize I had added content to it. So, from now on, I'm gonna keep editting this post with more questions, so just check back here for new ?s. This is the question I'm trying to solve for now (not hw assignment, prep question for midterm): Write the function interleave that merges two lists, alternating the members. If one list is longer, its extra elements simply get added to the end. The first argument's element goes first. >(interleave ()'(3)) (3) >(interleave '(3 5 7) '(4 6 8)) (3 4 5 6 7 8) I attempted it but came away with this heinous code: (define (interleave a b) (cond ((null? a) b) ((null? b) a) (else (cons (car a) (car b))))) ; (interleave (cdr a) (cdr b))))) First, I know I have to insert a recursive call somewhere. Secondly, I know that cons will not suffice, so perhaps I need to use append. But if I do that, then it takes the whole list and appends them together. So.. I'm stuck. Help's appreciated! (Will post more questions soon). -Edit-------------------- OKay, worked on this one and I keep getting worse at it. Here it is: Using only calls to sequence operators, define a procedure somof that takes an integer n as argument and returns the sum of the first n positive multiples of 5, starting with zero. You can use small lambdas to feed your seq. ops. >(somof 5) 75 I did this: (define (sumof n) (accumulate + 0 (enumerate 0 n))) This enumerates 0 to n. So I know I need to filter out multiples of 5 and then use accumulate on that. We have this proc: (define fives? (lambda (x) (= 0 (remainder x 5)))) (Of course we're allowed to use accum., filter, enumerate, fives..) I'm guessing I'm not setting up the procs properly is why I'm not getting correct results. TIA. [Edited by - red-dragonX on March 13, 2006 11:40:32 PM]

##### Share on other sites
marijnh    182
The first one just requires you to do (cons (cons ...) ...).
The second is probably easier to solve with recursion than with list comprehension, but it is possible both ways. Apply (lambda (x) (* 5 x)) to (1 .. n) and then sum the result.

##### Share on other sites
Roboguy    794
Quote:
 Original post by red-dragonXhey all,thanks for help with last post. I would've just added the content of this post as the edit of the last, but wasn't sure if you guys would realize I had added content to it.So, from now on, I'm gonna keep editting this post with more questions, so just check back here for new ?s.This is the question I'm trying to solve for now (not hw assignment, prep question for midterm):Write the function interleave that merges two lists, alternating the members. If one list is longer, its extra elements simply get added to the end. The first argument's element goes first.>(interleave ()'(3))(3)>(interleave '(3 5 7) '(4 6 8))(3 4 5 6 7 8)I attempted it but came away with this heinous code:(define (interleave a b) (cond ((null? a) b) ((null? b) a) (else (cons (car a) (car b))))) ; (interleave (cdr a) (cdr b)))))First, I know I have to insert a recursive call somewhere. Secondly, I know that cons will not suffice, so perhaps I need to use append. But if I do that, then it takes the whole list and appends them together. So.. I'm stuck. Help's appreciated! (Will post more questions soon).

Well, I believe you will need something that looks like this:
(cons ... (cons ... (interleave ...)))

in the recursive case.

Quote:
 -Edit--------------------OKay, worked on this one and I keep getting worse at it. Here it is:Using only calls to sequence operators, define a procedure somof that takes an integer n as argument and returns the sum of the first n positive multiples of 5, starting with zero. You can use small lambdas to feed your seq. ops.>(somof 5)75I did this:(define (sumof n) (accumulate + 0 (enumerate 0 n)))This enumerates 0 to n. So I know I need to filter out multiples of 5 and then use accumulate on that. We have this proc:(define fives? (lambda (x) (= 0 (remainder x 5))))(Of course we're allowed to use accum., filter, enumerate, fives..)I'm guessing I'm not setting up the procs properly is why I'm not getting correct results. TIA.

I believe (enumerate a b) will create a list starting at a and ending at b - 1 not b.

##### Share on other sites
red-dragonX    122
I'm still lost with
>(somof 5)

anyone wanna help?

##### Share on other sites
Roboguy    794
Quote:
 Original post by red-dragonXI'm still lost with >(somof 5)anyone wanna help?

Ah, misread your problem. Try applying filter (to filter out everything but multiples of five) to the result of the enumerate call.

##### Share on other sites
flangazor    516
I tried writing sumof5 with only using sequence operators and found it really tedious. You really can't use +, -, *, or / ?

##### Share on other sites
red-dragonX    122
Quote:
 Original post by flangazorI tried writing sumof5 with only using sequence operators and found it really tedious. You really can't use +, -, *, or / ?

well, if I could, it'd be a lot easier for me (I think) but the questions asks for using the sequence operator..

##### Share on other sites
Flarelocke    410
Quote:
 Original post by RoboguyWell, I believe you will need something that looks like this:(cons ... (cons ... (interleave ...)))in the recursive case.

Actually, you can do it with just one cons if you flip the arguments each time.

##### Share on other sites
You are very unclear about what restrictions you have for the second problem. You asked that we use only 'sequences operators'. That basically means that we must construct the natural number system from lists.

Here's the best I could do:

(define zero '())(define (zero? x) (null? x))(define (inc n) (cons '() n))(define (dec n) (cdr n))(define (fold combiner accumulator list)  (if (null? list)      accumulator      (fold combiner (combiner (car list) accumulator) (cdr list))))(define (unfold pred seed-value next-seed seed)  (if (pred seed) '()      (cons (seed-value seed)	    (unfold pred seed-value next-seed (next-seed seed)))))(define (add x y)  (fold cons x y))(define (multiply x y)  (fold add zero (map (lambda (z) x) y)))(define (sum list)  (fold add zero list))(define (enumerate n)  (unfold (lambda (x) (zero? x))	  (lambda (x) x)	  (lambda (x) (dec x))	  n)); (sum-of y n) === y + y*2 + ... + y*n(define (sum-of y n)  (sum (map (lambda (x) (multiply y x))	    (enumerate n))))

I have a feeling this is not the intent of your professor (or whomever gave you this assignment); it is likely that he wanted you to make use of Scheme's numerals. Well, too fucking bad. You should have written clear English you fucking troglodyte.

##### Share on other sites
red-dragonX    122
Quote:
 Original post by The Reindeer EffectYou are very unclear about what restrictions you have for the second problem. You asked that we use only 'sequences operators'. That basically means that we must construct the natural number system from lists.Here's the best I could do:*** Source Snippet Removed ***I have a feeling this is not the intent of your professor (or whomever gave you this assignment); it is likely that he wanted you to make use of Scheme's numerals. Well, too fucking bad. You should have written clear English you fucking troglodyte.

you need not be an ass!

##### Share on other sites
flangazor    516
Quote:
 Original post by The Reindeer EffectYou are very unclear about what restrictions you have for the second problem. You asked that we use only 'sequences operators'. That basically means that we must construct the natural number system from lists.Here's the best I could do:*** Source Snippet Removed ***

I came to the same conclusion about having to recreate numerals using lambda calulus and didn't think the prof really wanted his students to do this on an exam. I also thought he wanted input/output in numerals (1, 2, 3...) instead of lists like ('(()), '(() ()), '(() () ())) so I gave up.

Nice work.
Quote:
 I have a feeling this is not the intent of your professor (or whomever gave you this assignment); it is likely that he wanted you to make use of Scheme's numerals. Well, too fucking bad. You should have written clear English you fucking troglodyte.
Crappy specifications are part of life. [grin]

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account