Results 1 to 2 of 2

Thread: How to create an inverse function in Lisp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default How to create an inverse function in Lisp

    I'm using Dr. Scheme and need to translate the following function (given a + bi is an imaginary number) into Scheme:
    a - bi / (a^2) + (b^2)

    Here's what I already have:
    (define complex (lambda (a b)
    (cons a (cons b '()))))

    (define getReal (lambda (n)
    (car n)))

    (define getImaginary (lambda (n)
    (cadr n)))

    (define x (complex 1 2))
    (define y (complex 3 4))

    (define add (lambda (x y)
    (cons (+ (car x) (car y)) ( list (+ (cadr x) (cadr y))))))

    (define addnice (lambda (x y)
    (complex (+ (getReal x) (getReal y))
    (+ (getImaginary x) (getImaginary y)))))


    (define subtract (lambda (x y)
    (cons (- (car x) (car y)) ( list (- (cadr x) (cadr y))))))

    (define subtractnice (lambda (x y)
    (complex (- (getReal x) (getReal y))
    (- (getImaginary x) (getImaginary y)))))

    (define multiply (lambda (x y)
    (cons (- (* (car x) (car y)) (* (cadr x) (cadr y))) (list (+ (* (car x) (cadr y)) (* (car y) (cadr x)))))))

    (define multiplynice (lambda (x y)
    (complex (- (* (getReal x) (getReal y))
    (getImaginary x) (getImaginary y))
    (+ (* (getReal x) (getImaginary y))
    (* (getImaginary x) (getReal y))))))

    the "nice" versions of the functions are simply the easier way to do them.





  2. #2
    Erik's Avatar Dux Limitis
    Join Date
    Aug 2004
    Location
    Amsterdam
    Posts
    15,653

    Default Re: How to create an inverse function in Lisp

    You realize complex numbers are already implemented in Scheme?
    You can simply write complex numbers like 2+5i or 6-8i and use them like any other number.
    Or did you have to implement complex numbers for a school assignment?

    I'm not sure what you're asking but a - bi / (a^2) + (b^2) translates simply into (/ (- a bi) (+ (* a a) (* b b)))



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •