more scheme questions

Started by
9 comments, last by flangazor 18 years, 1 month ago
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]
Advertisement
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.
Quote:Original post by red-dragonX
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).

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)
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.


I believe (enumerate a b) will create a list starting at a and ending at b - 1 not b.
I'm still lost with
>(somof 5)

anyone wanna help?
Quote:Original post by red-dragonX
I'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.
I tried writing sumof5 with only using sequence operators and found it really tedious. You really can't use +, -, *, or / ?
Quote:Original post by flangazor
I 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..
Quote:Original post by Roboguy
Well, 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.
---New infokeeps brain running;must gas up!
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.
Quote:Original post by The Reindeer Effect
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:

*** 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!

This topic is closed to new replies.

Advertisement