I have data representation of complex numbers, but I don't know how to multiply two complex numbers. Maybe, someone can help me?
(define (complex-num a b)
(cons a b))
(define (real x)
(car x))
(define (imag x)
(cdr x))
Let's try to multiply two complex numbers
(a + bi) * (c + di) = (a + bi) * c + (a + bi) * di
= ac + bci + adi + bdii
= ac + bci + adi - bd (here we use that i*i = -1)
= ac-bd + (bc+ad)i
If we put z1=a+bi and z2=c+di then we can translate this to Scheme:
(define (multiply z1 z2)
(let ([a (real z1)]
[b (imag z1)]
[c (real z2)]
[d (imag z2)])
(complex-num ..compute ac-bd.. ..compute bc+ad.. )))
(define (complex-mult a b)
(make-rectangular (- (* (real-part a) (real-part b)) (* (imag-part a) (imag-part b)))
(+ (*(real-part a) (imag-part b)) (*(real-part b) (imag-part a)))))
e,g:
]=> (complex-mult 2+3i 5+2i)
;Value: 4+19i
]=> (complex-mult -i +i)
;Value: 1
Related
what is the required recursive function(s) in Scheme programming language to compute the following series?? Explanation needed
1^2/2^1 + 3^4/4^3 + 5^6/6^5 + 7^8/8^7 + 9^10/10^9
So, well, what does each term look like? It's n^(n+1)/(n+1)^n. And you want to stop when you reach 10 (so if n > 10, stop). So write a function of a single argument, n, which either:
returns 0 if n > 10;
adds n^(n+1)/(n+1)^n to the result of calling itself on n + 2.
Then this function with argument 1 will compute what you want. Going backwards may be easier:
return 0 if n < 1;
add n^(n+1)/(n+1)^n to the result of calling itself on n - 2;
then the function with argument 10 is what you want.
Or you could do this which is more entertaining:
(define s
(λ (l)
((λ (c i a)
(if (> i l)
a
(c c
(+ i 2)
(+ a (/ (expt i (+ i 1))
(expt (+ i 1) i))))))
(λ (c i a)
(if (> i l)
a
(c c
(+ i 2)
(+ a (/ (expt i (+ i 1))
(expt (+ i 1) i))))))
1 0)))
But I don't recommend it.
//power function
(define (power a b)
(if (zero? b) //base case
1
(* a (power a (- b 1))))) //or return power of a,b
// sum function for series
(define (sum n)
(if (< n 3) //base case
0.5
(+ (/ (power (- n 1) n) (power n (- n 1))) (sum (- n 2 )) ))) //recursion call
>(sum 10) // call sum function here .
I have a scheme function like so that generates a hash value for a given input
(define hash
(lambda (c)
(cond
((null? c) 600)
(else
(reduce + (map (lambda (x) (cv x)) c) (* 12 (hash (cdr c))))))))
cv(x) is where each letter maps to a number a = 1, b = 2, c = 3 ... z = 26.
600 is the base value.
12 is a unique number.
My problem is I'm doing something wrong that my values are a bit off and can't find where the problem relies.
Expected Output
(hash '(h i))
==> 86516
My Output
(hash '(h i))
==> 86525
This is what I'm trying to do :
600 * 12 + 9(val for i) = 7209
then,
7209 * 12 + 8(val for h) = 86516
As you can see my values are a bit off, I suspect how I'm using the reduce function.
You have a recursion inside reduce, while reduce is a high level function. No.
A simple recursion will suffice:
(define hash
(lambda (c)
(if (null? c)
600
(+ (cv (car c)) (* 12 (hash (cdr c)))))))
(hash '(h i)) ; => 86516
If, on the other hand, you want to use a high level function, you could use either foldr, as in:
(define hash
(lambda (c)
(foldr (lambda (x y) (+ (cv x) (* 12 y))) 600 c)))
or foldl, as in:
(define hash
(lambda (c)
(foldl (lambda (x y) (+ (cv x) (* 12 y))) 600 (reverse c))))
I have the following recursive function that I need to convert to iterative in Scheme
(define (f n)
(if (< n 3) n
(+
(f (- n 1))
(* 2 (f(- n 2)))
(* 3 (f(- n 3)))
)
))
My issue is that I'm having difficulty converting it to iterative (i.e. make the recursion have linear execution time). I can think of no way to do this, because I just can't figure out how to do this.
The function is defined as follows:
f(n) = n if n<3 else f(n-1) + 2f(n-2) + 3f(n-3)
I have tried to calculate it for 5 linearly, like so
1 + 2 + f(3) + f(4) + f(5)
But in order to calculate say f(5) I'd need to refer back to f(4), f(3), f(2) and for f(4) Id have to refer back to f(3), f(2), f(1)
This is a problem from the SICP book.
In the book, authors have an example of formulating an iterative process for computing the Fibonacci numbers.
(define (fib n)
(fib-iter 1 0 n))
(define (fib-iter a b count)
(if (= count 0)
b
(fib-iter (+ a b) a (- count 1))))
The point here is that use two parameter a and b to memorise f(n+1) and f(n) during computing. The similar could be applied: we need a, b, c to memorise f(n+2), f(n+1) and f(n)
;; an interative process implementation
(define (f-i n)
;; f2 is f(n+2), f1 is f(n+1), f0 is f(n)
(define (interative-f f2 f1 f0 count)
(cond
((= count 0) f0)
(else (interative-f
(+ f2 (* f1 2) (* f0 3))
f2
f1
(- count 1)))))
(interative-f 2 1 0 n))
(let ((a 3))
(let ((a 4)
(b a))
(+ a b)))
The above code evaluates to 7 the logic being that b takes the value of outer a. According to my understanding, in lexical binding each use of 'let' creates a fresh location. So why is the variable b in the statement (b a) not using the value of a from (a 4)?
Because that's what LET is specified to do. Bindings are done in parallel.
CL-USER 60 > (let ((a 3))
(let ((a 4)
(b a))
(+ a b)))
7
The version where bindings are done in a sequential fashion is called LET*.
CL-USER 61 > (let ((a 3))
(let* ((a 4)
(b a))
(+ a b)))
8
See Special Operator LET, LET*.
(let ((a 4)
(b a))
(+ a b)) ; ==> 7
Is equivalent to writing:
((lambda (a b)
(+ a b))
4
a) ; ==> 7
Do you see from this version that it's logical that a and b are bound after the evaluation of 4 and a?
Now we have:
(let* ((a 4)
(b a))
(+ a b)) ; ==> 8
which is equivalent to:
(let ((a 4))
(let ((b a))
(+ a b))) ; ==> 8
Here the second let is in the body of the first. a is 4 when the expression for b is evaluated.
I'm attempting to write a program in Common Lisp that dynamically creates other lisp files. Common Lisp's print function seems very useful for this purpose. Unfortunately, the function outputs data on a single line. For example (just printing to standard output):
(print '(let ((a 1) (b 2) (c 3)) (+ a b c)))
>> (let ((a 1) (b 2) (c 3)) (+ a b c))
The generated lisp files need to be human readable and thus shouldn't minimize whitespace. It seems that the pprint function is the solution to my problem. Since pprint sets *pretty-print* to true, the function should print on multiple lines. In other words:
(pprint '(let ((a 1) (b 2) (c 3)) (+ a b c)))
>> (let ((a 1)
>> (b 2)
>> (c 3))
>> (+ a b c))
However, in Allegro CL, pprint seems to behave in an identical manner to print. Output is only on a single line. Is there a way to cause the function to print s-expressions in a "pretty" way? Are there any other globals that need to be set before the function prints correctly? Is there an alternative function/macro that I'm looking for? Thanks for the help!
The pretty printer is controlled by more than just *print-pretty*. E.g., look at the interaction with *print-right-margin* in SBCL (under SLIME):
CL-USER> (pprint '(let ((a 1) (b 2) (c 3)) (+ a b c)))
(LET ((A 1) (B 2) (C 3))
(+ A B C))
; No value
CL-USER> (let ((*print-right-margin* 10))
(pprint '(let ((a 1) (b 2) (c 3)) (+ a b c))))
(LET ((A
1)
(B
2)
(C
3))
(+ A B
C))
; No value
CL-USER> (let ((*print-right-margin* 20))
(pprint '(let ((a 1) (b 2) (c 3)) (+ a b c))))
(LET ((A 1)
(B 2)
(C 3))
(+ A B C))
; No value
You might be able to get satisfactory results just by setting that variable, but in general you'll want to have a look at 22.2 The Lisp Pretty Printer. Pretty printing functions have lots of places for optional newlines and the like, and where they get put depends on a number of things (like *print-right-margin* and *print-miser-width*). There are some examples of using the pretty printer to format Lisp source code in 22.2.2 Examples of using the Pretty Printer. There's too much to quote it all, but it shows how the following pretty printing code can produce all these outputs, depending on context:
(defun simple-pprint-defun (*standard-output* list)
(pprint-logical-block (*standard-output* list :prefix "(" :suffix ")")
(write (first list))
(write-char #\Space)
(pprint-newline :miser)
(pprint-indent :current 0)
(write (second list))
(write-char #\Space)
(pprint-newline :fill)
(write (third list))
(pprint-indent :block 1)
(write-char #\Space)
(pprint-newline :linear)
(write (fourth list))))
(DEFUN PROD (X Y)
(* X Y))
(DEFUN PROD
(X Y)
(* X Y))
(DEFUN
PROD
(X Y)
(* X Y))
;;; (DEFUN PROD
;;; (X Y)
;;; (* X Y))