Subject: Re: voltage & current program
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 10 Apr 2007 04:05:24 -0500
Newsgroups: comp.lang.lisp
Message-ID: <NO-dnaPWQ6bJzobbnZ2dnUVZ_uSgnZ2d@speakeasy.net>
Pascal Bourguignon  <pjb@informatimago.com> wrote:
+---------------
| "sandeep patil" <san.gujar@gmail.com> writes:
| > can you tell me any function in ohms law & it related program.
| > who i will right program on it.
| 
| Ohm's Law, for a resistor, is:
|    U = R * I  (voltage = resistance * intensity)
+---------------

FYI, in standard English engineering notation/language,
the symbol for voltage is "V", not "U". And the symbol "I"
refers to "current", not "intensity". And although this
doesn't matter at all to correctness, the rule is usually
given with "R" last [for unknown-to-me historical reasons,
I suppose]:

     V = I * R  (voltage = current * resistance)

+---------------
| You can write a function in Common Lisp to solve this equation,
| known at least two of its variables:
| 
| (defun resistor-ohm-law (&key u r i)
|   (cond
|      (u  (cond (r (list :i (/ u r)))
|                (i (list :r (/ u i)))
|                (t (list :|R(I)| (lambda (i) (/ u i))
|                         :|I(R)| (lambda (r) (/ u r)))))
+---------------

Yes, this is one place where I really, really like CL's keywords!!
But I generally write this kind of thing as follows, which feels to
me to be more perspicuous [though admittedly slightly more redundant
and thus *slightly* less efficient -- a tradeoff I'm usually willing
to make]:

  (defun resistor-ohm-law (&key v r i)
    (cond
      ((and v i r)
       (error "May not specify all three parameters!"))
      ((and v r) (list :i (/ v r)))
      ((and v i) (list :r (/ v i)))
      ((and i r) (list :v (* i r)))
      (t
       (error "Must specify at least two parameters!"))))


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607