LISP
1
Name:
Anonymous
2009-12-01 13:59
hey /prog, could you guys help me with assignment. I need to write a program in Lisp that does
Π((k-a)/(k+a))=((1-a)/(1+a))*((2-a)/(2+a))*...*((n-a)/(n+a)), k=1..n
k, a are read from stdin
2
Name:
Anonymous
2009-12-01 14:15
3
Name:
Anonymous
2009-12-01 14:18
4
Name:
Anonymous
2009-12-01 14:22
how do you "do" an equation?
5
Name:
Anonymous
2009-12-01 14:23
you've used k as a loop variable and you said is comes from input.
6
Name:
Anonymous
2009-12-01 14:24
>>4
>>5
No fucking wonder this idiot can't write it herself.
7
Name:
Anonymous
2009-12-01 14:36
"fuck i meant a and n are read from stdin"
8
Name:
Anonymous
2009-12-01 15:54
(defun f (a n)
(do ((k 1 (1+ k))
(prod 1 (* prod (/ (- k a) (+ k a)))))
((> k n) prod)))
9
Name:
Anonymous
2009-12-01 16:43
(define f
(λ(a k)
(/ (- k a) (+ k a))))
(define Π
(λ(a k)
(let Pi ((k′ 1)
(accum (f a 1)))
(if (= k k′)
accum
(let ((k′′ (+ 1 k′)))
(Pi k′′ (* accum (f a k′′))))))))
(define Π-reader
(λ()
(display "k = ")
(let ((k (read)))
(display "a = ")
(let ((a (read)))
(if (not (and (positive? k) (integer? k) (number? a)))
'error
(Π a k))))))
Welcome to DrScheme, version 4.2.1 [3m].
Language: Module custom; memory limit: 512 megabytes.
(Π-reader)
k = 10
a = 1/3
11339/82460
10
Name:
Anonymous
2009-12-01 16:48
(-1)^k * (a! * (a-1)!)/((a+k)! * k!)
11
Name:
Anonymous
2009-12-01 17:12
12
Name:
Anonymous
2009-12-01 18:39
>>11
That's possible, maybe I should have written it down.
13
Name:
Anonymous
2010-11-14 23:55