330 likes | 479 Views
Programming Languages. Informatics I101 March 22, 2004 John C. Paolillo. Computers. Programmable, general-purpose device performs any desired computation the program is an arrangement of logical relations of bits How does one accomplish the programming? Hard-wiring (ENIAC)
E N D
Programming Languages Informatics I101 March 22, 2004 John C. Paolillo
Computers • Programmable, general-purpose device • performs any desired computation • the program is an arrangement of logical relations of bits • How does one accomplish the programming? • Hard-wiring (ENIAC) • Machine instructions (“code”)
Interacting with Computers • Originally • Computer instructions are binary numbers • “machine code” • Low-level languages (“assembly language”) • Easily-remembered mnemonics translated directly into machine code
High-Level Languages • Compiled • Translated into machine code in two-steps • compile (into “relocatable object code”) • link (with various resources into machine code) • Not interactive • Interpreted • Translated into machine code and executed directly
Purpose Scientific, Other applications Interactive Shells Web support Typesetting Compiled FORTRAN Pascal C C++ TeX Perl, Java Interpreted LISP Pascal Unix Shell lgs DOS .BAT files JavaScript, C# PHP Postscript Some High-level Languages
User Interface Languages • Command-line systems • UNIX • DOS • VAX, TOPS-20 • MULTICS • Graphic User Interfaces • Windows, Icons, Menus and Pointers (WIMP) • Xerox Star • MacOS • Windows • X-Windows
What’s in a High-level Language? • Commands and built-in functions • Sequence/order of execution • Variables (to hold values) and assignment • Operators (especially for arithmetic) • Constructs for defining • Data structures • Algorithms (programs, subroutines, functions) • Control structures • if/then/else, loops • Recursion (through the program stack)
<?php // Solution to chart portion of PHP exercise 2. // John Paolillo, October 2003. $ncol = sizeof($_GET) ; // $_GET holds the histogram $mx = max($_GET) ; // maximum value $wd = 400 ; // width $ht = 200 ; // height $cw = $wd / ($ncol + 2) ; // column width $hh = $ht - 2 * $cw ; // maximum height (pixels) $ccc = $cw/2 ; // half-width (for labels) $b = $ht - $cw ; // bottom line $image = imagecreate($wd,$ht) ; // First color defined (red) is automatically the picture background. $red = imagecolorallocate($image, 255, 0, 0) ; $wht = imagecolorallocate($image, 255, 255, 255) ; ksort($_GET) ; // Sort the bars by key value while (list($key,$val) = each($_GET)) { $t = $b - ($hh * $val / $mx) ; // Bar height $l = $l + $cw ; // Bar left $r = $l + $cw ; // Bar right imagefilledrectangle($image,$l,$t,$r,$b,$wht) ; imagestring($image,1,$l+$ccc,$ht-$ccc,"$key",$wht) ; } // Chart title imagestring($image,1,$cw,$ccc, "Distribution of Amino Acids in Peptide",$wht) ; // Output as PNG & clean up. header("Content-type: image/png") ; imagepng($image) ; imagedestroy($image) ; ?>
Programming Paradigms • Procedural • Step-wise procedures • FORTRAN, BASIC, C, Pascal • Functional • Mathematical functions • Function composition, no assignments • Scheme, XSLT • Object-Oriented • Algorithms and data “encapsulated” together • Objects arranged in a hierarchy of inheritance • SmallTalk, C++, • Logic Programming • Logical relations • Prolog
Procedural: PHP function factorial($n) { if ($n == 1) { return 1 ; } else { return n * factorial(n-1) ; } }
Procedural: Postscript /factorial { dup 1 gt { dup 1 sub factorial mul } if } def
Functional: LISP (defun fact (x) (if (<= x 0) 1 (* x (fact (- x 1)))))
Functional: APL X
Functional: J factorial=. 1:`(]*factorial@<:) @. *
Functional: XSLT <xsl:template name=“factorial”> <xsl:param name=“val”/> <xsl:choose> <xsl:when test=“$val=‘1’”> <xsl:value-of select=“1”/> </xsl:when> <!-- end base case --> <xsl:otherwise> <xsl:variable name=“factless”> <xsl:call-template name=“factorial”> <xsl:with-param name=“val” select=“$val - 1”/> </xsl:call-template> </xsl:variable> <xsl:value-of select=“$val * $factless”/> </xsl:otherwise> <!-- end recursive case --> </xsl:choose> </xsl:template>
Logic Programming: Prolog factorial(1,1) :- !. factorial(N,FactN) :- M is N - 1, factorial(M,FactM), FactN is M * FactM.
http://www.digibarn.com/collections/posters/tongues/ComputerLanguagesChart-med.pnghttp://www.digibarn.com/collections/posters/tongues/ComputerLanguagesChart-med.png
C++: http://gcc.gnu.org/ Sun’s Java Technology The ECMAScript Language Specification ECMA-262.pdf Programming Language Naming Patterns Collection of Programming languages: [99 Bottles of Beer] - Section A An unusual language: Brainf***