most recent semester
lect01
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?
-
learn other approaches; lets you understand your native tongue better;
-
practice at learning new technologies/ideas;
-
makes you think about: when is language helping, and when getting in the way;
e.g. sorting: scheme vs Java
-
Help you choose a language appropriate to the task
You will approach problems differently
-
numeric example,
java, python, racket, mathematica; java-bigint
Why?
Takeaway: You now understand arithmetic better than before; it's because you saw different approaches.
Knuth: "CS is seeing the forest and the trees simultaneously."
Think: how to implement; implement efficiently?
Does it matter? WHat are situations where efficiency should override correctness?
order-of-magnitude; real-time;
Don't optimize prematurely
-
lookup example: hash tables vs lists vs arrays (java, python, php, scheme (a-list), racket (hash))
...pyret
Takeaway: what the language provides influences how you "bother" to code your task,
even if it's not the most appropriate way!
-
recognize features that sound good, but turn out not to be:
mult inherit (C++); prototype (javascript); text-level pre-processor (C)
If projects have style guides "don't use these features of the language", that's a sign.
Includes better ways: public/private/protected; vs namespaces, packages, exports
-
- recognize unnecessary idiosyncracies:
java's cruft (public static void main; ...)
javascript examples: {} + [], ...
(def'n of low-level)
categories of languages … and, why to ignore them.
Also: "language X good for Y" (e.g. “Fortran good for scientific computing”,
“Lisp good for AI”)
is better thought of as "feature X helpful with Y":
“optimized numeric libraries are good for scientific computing”;
“lists and symbols are helpful for AI”
But still: memorize: ~12 notable languages w/ a defining feature —
because people expect you to know the community consensus.
Discuss:
You've used Java, and (others like) Ada, and javascript, and php, and C.
What are features of Java you like,
and when are you writing something where the language feels like it's getting in the way?
-
When you read input from the user, and need to make sure it's an integer in [70,80).
-
You want to sort a list of strings
(say, all the state-names that your company has a "business presence" in).
-
First: how do we just represent this data? (what type?)
Note: avoid the double-bracket “notation for initializing”
as mentioned on some blog posts —
it's doing something very different that happens to include the initializatlin you want, but (by
creating anonymous inner classes) is also doing things you don't want.
Or better:
or the same thing indented differently:
-
Okay, how do we sort that, alphabetically?
-
What if you want to sort a list of numbers, according to your own sorting-criterion?
(I'll use "descending", but you can see where you can change this to be anything:
java.util.Collections.sort(data3, new java.util.Comparator<Integer>(){ public int compare(Integer a, Integer b) { if (a.intValue() < b.intValue()) return 1; else return -1;} });
See below, for more on using Comparator.
This took me a while to get right: in particular,
I forgot I needed the () (the constructor-args) before the class-definition,
and also I had to upgrade the code from using ints to Integers to
get the anonymous Comparator to work.
-
You want to sort a list of Memes.
class Meme {
File imageFile; // e.g. first-world-problem.jpg or mostInterestingManInTheWorld.gif
String topText;
String bottomText;
}
|
Sorting involves first by timestamp of imageFile,
resolving ties by topText,
resolving ties by bottomText.
Or, in a different language: Racket:
Writing this in racket, though, makes me think:
Hey, < can be applied not just to exactly two arguments (inputs),
but to any number of inputs: For example (< 2 3 4) is true,
and (< 2 3 99 4) is not.
Gee, shouldn't I write a version of meme<? which can take in any
number of memes, and say whether or not they're ordered?
It turns out, since racket evolved from a language for list-processing, this isn't so difficult
after all.
By mid-semester, you'll be able to write racket code like this:
It's worth noting that this generalization in Java (pre-Java-8) is
-
More verbose — each map needs to be a for-each loop —
-
Not as generally useful (!):
because generalizing from 2 to an entire list (collection) is so much harder,
other people haven't written handy library functions that can make use of this more general code!
This is a serious point: becase map is so easy to use, racket programmers
use it all the time.
Java programmers can do the same thing, but because it has (just) a couple extra lines of overhead
per use, they don't,
and even if they did take the time to write more general code it won't get leveraged by others.
-
What if you want to sort Memes in different ways, depending:
sometimes by date of file, but sometimes by just the text, sometimes ... .
Three ways to consider, in a language:
- Syntax (what strings are legal programs?)
- Semantics (what is the meaning of the program?)
- Pragmatics
(what is in/convenient and un/suprising,
about different syntactic constructs, and their semantics)
The textbook also talks more broadly, about the costs associated with
the choice of various languages.
Families of languages:
- low-level
- procedural
- object-oriented
- scripting
- functional
- declarative
These are not mutually exclusive!
Reading: Chapter 1, by Friday.
Note: Book; add/drop.
A history overview
name yr family feature(s)/notables (descendents/related) example
TM, lambda calculus
machine / hardware switches (incl. Babbage's Analytic Engine)
assembly
Lisp; Scheme, ML, Haskel, F#
Fortran; PL/I; Algol;
SmallTalk; C++, java, C#
Ada;
sh, csh, Tcl/Tk, perl, python, ruby
rpn; postscript (printer is the interpreter; req'd M68000, not 8088,6502); pdf
prolog (Japanese "4th wave")
Basic (Apple II ROM; line#s; goto;'gosub' but no params (only globals) (recursion?!)),
http://www.jenutech.com/images/HomeSweetGoto10.gif
http://theinfosphere.org/File:Gotohell.jpg
Pascal (teaching; nested procedures)
language popularity index
poster: programming languages history
The hello world collection
- examples: enscript -o foo.ps (wimpout.ps)
Example: enscript -o foo.ps ; cat foo.ps ; open foo.ps
Example: cat wimpoutBoard.ps ; open wimpoutBoard.ps
also, sh: Example: cd ~/Unix
find . -name \*.ps -print
find . -name \*.ps -print | grep -i wimpout
- other notables:
* postscript
- C, Ada, Fortran, Pascal, Rexx, ...
- Smalltalk, Java, C++, C#, ...
- APL; Mathematica; Matlab
- javascript, perl, etc; html ? -- no; data. But, .css.
- sh/csh, .bat, applescript; scriptable photoshop?
Example: cd ~/Unix
find \*.ps
find \*.ps | grep -i wimpout
Note that we are calling functions. (How are arguments passed?)
- a *programmable* calculator.
* MS Word "macros" and mailing-list scripts
- abacus-instructions ?
Link:
"A new language for phone networks" (declarative; based on F#)
www.technologyreview.com/communications/23330/?a=f
most recent semester