|
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?
Consider: 7 · 25 ÷ 25.
Also: 2.5 + x - x + 2.5.
What is √2 · √2 ?
(Give the racket expression.)
In sage:
You've probably guessed that the different behaviors above stem from how the data is represented
internally.
Java (and many languages) represent primitive
For arbitrary-precision integers,
you can keep track of a list-of-digits2
This is what racket and python do by default, and what Java uses
In the same vein, representing rational numbers exactly is straightforward, if you use a struct with two fields (the numerator and denominator).
(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
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)) { ... } |
What if you want to use an array? Java makes that much easier for you:
Integer[] nums2 = {22, 17, 24, 30, 5}; |
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
Integer[] nums2 = {22, 17, 24, 30, 5};
List<Integer> nums1 = java.util.Arrays.asList( nums2 );
|
Recent versions of Java finally allow even more succinct phrasings, which finally rival the conciseness of their array-syntax using mere method-calls:
List<Integer> nums1 = java.util.Arrays.asList( new Integer[] {22, 17, 24, 30, 5} ); // creates an *immutable* list! // In Java 10: var nums1 = java.util.List.of( 22, 17, 24, 30, 5 ); // creates an *immutable* list! |
Warning:Java's
asList method is documented as “Returns a fixed-size list...”. If you take its result and call (say) the.add method, it throws anUnsupportedOperationException — This is unintuitive behavior! When a method says it returns aList , the standard expectation is that it returns an object which supports the methods of interfaceList — that's what (abstract) types are all about! A developer shouldn't have to memorize which of the umpteenjava.util.List methods create Lists that don't actually support all the List methods.If they had named the method
asImmutableList , it would make sense. Or if they just had the method return an actual List (and copy the backing array), it would make sense. They decided to favor both runtime-performance and short names, at the expense of violating both the Principle of Least Surprise and the good-O.O. practice of avoidingUnsupportedOperationException s. (Also, the designers might have made a interface ImmutableList, to avoid a plethora of UnsupportedOperationExceptions.)
While Java priviliges strings and arrays with particularly simple syntax and rules,
Lisp priviliges lists (in fact, its name stems from “List Processing”).
Instead of constructing
(list (list 320 'ibarland 2018 'fall) (list 320 'nokie 2018 'fall) (list 380 'ibarland 2017 'fall) (list 220 'jchase 2018 'spring)) |
'((320 ibarland 2018 fall) (320 nokie 2018 fall) (380 ibarland 2017 fall) (220 jchase 2018 spring)) |
Furthermore: Lisp families have, in addition toquote , a back-quote (` ) which lets you do even more!
Note that if using arrays (“vectors” in Lisp),
there is a “long” constructor
(using
Special mention goes to python, for their convenient and mnemonic shorthand-syntax for some particularly-common (and useful) data types:
And a reminder that by seeing what other languages make easy or difficult, you gain the ability to see what your "primary" language is making easy or difficult — “You can never understand one language until you understand at least two.”.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |