Subject: Re: voltage & current program
From: (Rob Warnock)
Date: Tue, 10 Apr 2007 04:05:24 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
Pascal Bourguignon  <> wrote:
| "sandeep patil" <> 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)
      ((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)))
       (error "Must specify at least two parameters!"))))


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607