RU beehive logo ITEC dept promo banner
ITEC 380
2016fall
ibarland

homelecturesrecipeexamshwsD2Lbreeze (snow day; distance)

language-affects-choice--arithmetic
Why study programming languages?

You can never understand one language until you understand at least two.
John Searle
The limits of my language are the limits of my world.
—Ludwig Wittgenstein

Why study programming languages?

Languages steer programmer's decisions

Java encourages choosing bad integer arithmetic

Java, python encourage bad rational arithmetic

Consider: 7 · 25 ÷ 25.
Also: 2 + x - x + 3.

Java, python, racket encourage bad irrational arithmetic

What is √2 · √2 ? (Give the racket expression.)
In sage:

Note a different language issue, in that third example: we provided the argument “n(, digits=50)”, rather than just passing 50 as the second argument. That's “keyword arguments”; why/when might it be helpful? (It turns out python allows this, and sage is built on python.

Why the difference?

You've probably guessed that the different behaviors above stem from how the data is represented internally. Java (and many languages) represent primitive ints and floats using a fixed 32-bits (and using 64-bits for long and double). If a number can't fit in 32-bits, it can't be represented by that type.

For arbitrary-precision integers, you can keep track of a list-of-digits This is what racket and python do by default, and what Java uses java.math.BigInteger for.

In the same vein, representing

(In dynamically-typed languages: the machine representation might include a tag, which requires a few bits. For example, a 32-bit quantity starting with the bits 101 might happen to let the language know that this quantity is the start of a string. For common types, a shorter tag might be used; since int is a very common type, a single leading bit of 0 might indicate a small-integer (one that fits in the remaining bits), and that lets the language-implementation use existing integer-arithmetic-hardware for that value!)

Java encourages you to use arrays (even when lists are better)

Suppose you want to keep list of temperatures on recent days (in Celcius, of course).

    List<Integer> nums = new ArrayList<Integer>();
    nums.add(22);
    nums.add(17);
    nums.add(24);
    nums.add(30);
    nums.add(5);
    
    if (nums.contains(n)) {
      ...
      }
  
Oh my goodness!

What if you want to use an array? Java makes that much easier for you:

  Integer[] nums2 = {22, 17, 24, 30, 5};
So if you want to sort a bunch of items, it's concise to use an array, and verbose to use a List — Java is encouraging the programmer to use an array, even if a List is more appropriate.

It's worth mentioning that although Arrays are easy to declare, iterate over, and statically-initalize in Java, they're also a bit underdone: there are many useful functions for arrays that Java didn't include (not even when they added the utility-class java.util.Arrays); a contains method is one of them (!). You can download a third-party libraries (like guava or apache-commons (written since others were fed up with this shortcoming), or you can often convert the array to a list, and then use the standard java.lang.Collections methods:

  Integer[] nums2 = {22, 17, 24, 30, 5};
  List<Integer> nums1 = java.utils.Arrays.asList( nums2 );


1 Keyword arguments are allowed in racket, but they aren't automatic like they are in python. On the other hand, keywords are values, so you can pass/return keywords from functions.      

2Where each individual “digit” is perhaps a 32-bit quantity, or so.      

homelecturesrecipeexamshwsD2Lbreeze (snow day; distance)


©2016, Ian Barland, Radford University
Last modified 2016.Sep.11 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.