English 中文(简体)
How to solve the following equation using accumulate (Scheme)
原标题:
  • 时间:2009-11-13 14:39:38
  •  标签:
  • scheme

I m trying to do the following problem (there is a formula so I print-screened and uploaded it)

Formula http://img248.imageshack.us/img248/6558/problemh.jpg
(http://img248.imageshack.us/img248/6558/problemh.jpg)

Using accumulate:

(define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b))))

but I have pretty much no idea how to do it. It should look something like (define (sum-func f k)...? I m not sure how I should define f(x+ j*h) so that to put the changing number j (changes from 0 to the given number k) inside of it... In other words, I m pretty lost.


Thank you for the fast respond. I m studying for my midterms and I m trying to do the midterms from the previous years, but I just got stuck here. I know how to use the accumulate and everything in it, but I just don t know how to do the f(x + jh). I m trying this: (define (sum-func term a b h x) (define (next a) (+ a 1)) (define (term a) (term (+ x (* h a)))) (accumulate + 0 (term a) a (next a) b)) But it s not working... In other words I don t know how to use the next (changing part) inside the term.. (sorry if I m explaining very clearly - English is not my native)

问题回答

I am not sure if this is a homework problem or not but in any case I will provide you with some ideas on how to solve it. For the sake of simplicity let us assume our problem is to find the sum of squares of all numbers between j and k. You can use your accumulate function above to solve this simple problem. All you will have to do is define your combiner, term and next functions for this problem along with defining the final null-value.

Term function here should calculate the squares for each j. Therefore term will take j as a argument & will return the square of j as the result. (define (term j) (sqr j))

Next function should get the next j in the sequence. (define (next j) (+ j 1)

Combiner function should combine two terms together. (define (combiner t1 t2) (+ t1 t2))

Finally, null-value signifies the stop condition, the last value that should be passed to combiner when we have accumulated all the values from j to k. All we have to do is just define it as a zero in this case. (define null-value 0)

For your problem, most of these functions are same except for the term function. In this problem, the term function was a very simple one, it just found the square of the supplied number. However, in your case, it is not as simple as finding the square of j since you have several other constants defined.

Hope this will help you solve the problem.

If you need even more insight into this, you may want to look at the accumulate problems in SICP chapter 2.

I figured it out. I guess I was overthinking the algorythm (thinking it was much more complex than it was) and this led me to many stupid mistakes.

(define (accumulate combiner null-value term a next b)


(if (> a b) null-value


(combiner 
      (term a)
      (accumulate combiner null-value term (next a) next b))))


(define (sum-row term x h n)

  (accumulate + 0
              (lambda (t) (* (if (even? (- n t)) 1 -1)(term (+ x(* h t))))) 
              0 
              (lambda (t) (+ t 1))
              n))


(define (square x) (* x x))

(sum-row square 2 2 3)

; -4+16-36+64=40
; -f(2+0*2)=-4 f(2+1*2)=16 -f(2+2*2)=-36 f(2+3*6)=64




相关问题
Parsing with DCGs in Scheme (without Prolog)?

Lots of Prolog-in-Scheme implementations are out there. E.g. Kanren, Schelog. Apparently in "Paradigms of AI Programming" Norvig implements Prolog-to-Lisp compiler in Lisp in order to use Definite ...

Applying a symbol as a procedure

Suppose I have a simple symbol: > + + Is there any way I can apply that symbol as a procedure: > ((do-something-with +) 1 2) 3 So that + is evaluated to the procedure +?

string-split in DrScheme

How do I do equivalent of python s str.split in DrScheme? SRFI-13 doesn t seem to have it provided.

Scheme, getting the pointer from pointed struct

Assume I have a such struct: (define-struct node (value next)) ;and making 2 nodes, parent pointing to child as next. (define child (make-node 2 null)) (define parent (make-node 1 child)) Under ...

How to solve the following equation using accumulate (Scheme)

I m trying to do the following problem (there is a formula so I print-screened and uploaded it) Formula http://img248.imageshack.us/img248/6558/problemh.jpg (http://img248.imageshack.us/img248/6558/...

热门标签