Jump to content

Recommended Posts

Posted

I have variable a that i want to divide by two then add one. I can divide and I can add but im not sure how to divide and add, do I need another variable?

Posted

Lisp is a very clean language to work with with the exception of basic algebra...

 

Addition, subtraction, multiplication and division all work on a similar concept: they're all a function, and must be treated as such.

 

So, if you want to divide a number "N" by two then add one, you need to break it up and use the proper function.

 

(setq result (/ N 2))

Will divide your number by two.

 

(+ result 1)

Will add one to the result of that. So you can see, if you simply replace the variable with the function, you'll get:

 

(+ (/ N 2) 1)

Which is very, very messy in the eyes of anyone who has done a lot of math (or just mine :P), but that's how LISP does it.

 

I hope my ramblings have helped. ^^

Posted

they helped alot, thanks for the explanation and not just the answer!

Posted

Chelsea, another thing - be careful with divide... and I don't just mean about not supplying a zero denominator...

 

Divide is very sensitive to the difference between INT and REAL arguments (Integers and Reals).

 

Hence:

 

(/ 1 2)   ---> Returns 0

 

(/ 1 2.0)   ---> Returns 0.5

 

(/ 1.0 2.0)   ---> Returns 0.5

 

(/ 1. 2)   ---> Returns 0.5

 

 

Hence, if you supply two INT's as arguments, it will return an INT.

Posted

You're very welcome, always happy to help. ^^

 

Going further, there is a reason LISP does things this way. Since it's a function, it can accept many inputs, so if you're adding many numbers together, LISP is head and shoulders above the rest, id est:

 

(setq var (+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 .... ))
compared to
set var = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 ...

 

But when it comes to complex equations requiring many different operations, you, as Lee Macs signature used to say, get Lost In Superfluous Parentheses. This is especially difficult when you're going to attempt tracking a projectile particle through a viscous fluid... >.>'

 

Pros and cons, double-edged swords, all that jazz. LISP is still nice even if algebra is a little wronky. :3

Posted

Nice one FreeRefill, not trying to trump you, but also with LISP, if you wish to sum a list of numbers quickly, as "+" is treated as just another function in your inventory, you can use it with apply:

 

(setq lst '(1 2 3))

(apply '+ lst)  ---> Returns: 6

Posted

Im having user input a duct size which will be entered as an integer, to make up for that when i divide by 2 should i really divide by 2.0 to take into account for that so if its a 9" duct it will output 4.5 not 4

(/ A 2.0); where A is an int

and not

(/ A 2); now A and 2 are int

Posted

Exactly - always be aware of what data types you are using for what :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...