RU beehive logo ITEC dept promo banner
ITEC 380
2016summerIII
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 python1 allows this, and sage is built on python.

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.      

homelecturesrecipeexamshwsD2Lbreeze (snow day; distance)


©2015, Ian Barland, Radford University
Last modified 2016.May.17 (Tue)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.