Document 239894

Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Why learn about programs?
Why learn about programs?
Programs
László Kálmán1
Csaba Oravecz1
Péter Szigetvári2
1 ELTE/MTA
Theoretical Linguistics Programme;
Research Institute for Linguistics, Hungarian Academy of Sciences
2 Department
of English Linguistics
Eötvös Loránd University
1
for knowing what a program is
2
for intellectual pleasure
(programming is the best of computer games!)
3
for not being fouled by gurus
4
for writing small applications
(at least some web programming)
Lecture 9. / 12 Nov, 2008
Kálmán, Oravecz, Szigetvári
Introduction
Meet your first program
Kálmán, Oravecz, Szigetvári
Programs
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
What is a program, after all?
Source code
What is a program, after all?
A sample script
Programs
Elements of programming
Programming style
A simple program
TVMRX,IPPS[SVPH
1
a data file that a computer can understand
(it can be executed, and then interesting things may
happen)
2
a sequence of instructions that a computer can carry out
3
the description of an algorithm that can be implemented in
a computer
4
the abstract description of processes that can take place in
a computer
program = algorithm + data structure
(but the data themselves are not part of the program)
Kálmán, Oravecz, Szigetvári
Programs
of course, this doesn’t do anything interesting
to execute this program, the computer must know how to
interpret it (what language it is in)
assume the program is saved in a file called
helloworld.py (or whatever else)
to invoke this program in a CLI, type
> python helloworld.py
you get
Hello, world!
>
(the script was written in the Python programming
language)
Kálmán, Oravecz, Szigetvári
Programs
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
Elements of programming
Source code
Executable code and source
Some other languages
Two looks of programs
C
Programming style
A simple program
C or C++ programs cannot be directly executed by the
computer (not even by another program).
MRGPYHI WXHMSL"
QEMR
_
TVMRXJ,IPPS[SVPH@R
a
Some concepts
source (code): the program as you type it in
script: a source that can be interpreted directly by a
program
(e.g., Python is a scripting language)
C++
MRGPYHI MSWXVIEQ"
QEMR
_
WXHGSYX
a
,IPPS[SVPH
Kálmán, Oravecz, Szigetvári
Introduction
Meet your first program
interpreter: a program that can interpret scripts
(e.g., python is the name of a Python interpreter)
WXHIRHP
Kálmán, Oravecz, Szigetvári
Programs
Elements of programming
Programming style
A simple program
Executable code and source
Introduction
Meet your first program
Programs
Elements of programming
Programming style
Executable code and source
Compilation (translating source into executable)
This is how the “helloworld” executable (from the C++
source) looks:
In a CLI (under Linux):
> g++ -o helloworld helloworld.cpp
>
g++ is the name of the C++ compiler;
-o helloworld means the executable file will be called
helloworld
helloworld.cpp means the source file is called
helloworld.cpp
Using the executable:
> helloworld
Hello, world
>
Kálmán, Oravecz, Szigetvári
Programs
Kálmán, Oravecz, Szigetvári
Programs
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
Executable code and source
Executable code and source
Compilation (translating source into executable)
Script languages vs. compiled languages
In a CLI (under Linux):
> g++ -o helloworld helloworld.cpp
>
compiled code is also interpreted by the computer, but it is
chewed beforehand (during compilation), so it runs faster
g++ is the name of the C++ compiler;
-o helloworld means the executable file will be called
helloworld
helloworld.cpp means the source file is called
helloworld.cpp
Using the executable:
> helloworld
Hello, world
Advantages of compiling
compiled program runs much faster
compiler finds many errors
(but runtime errors can be mysterious: you must use a debugger
to find out their reason)
Advantages of scripting
easier to write (less explicit, e.g., in declarations)
runtime error messages are more informative
>
Kálmán, Oravecz, Szigetvári
Introduction
A simple program
Meet your first program
Kálmán, Oravecz, Szigetvári
Programs
Elements of programming
Programming style
A simple program
Program files
Introduction
Meet your first program
Programs
Elements of programming
Program files
Structure of a program
a header file
MRGPYHI WXHMSL"
HIJMRI1%<-191C0-2)0)2+8,
Types of program files
A famous type is called a header. It contains useful stuff
(but usually no algorithm).
It is kept separate because many other files may include it.
(remember the first line of our C and C++ programs?)
#include <stdio.h>
#include <iostream>
“Normal” files containing algorithms
Previously compiled parts of programs that can be used by
others, without recompilation: object files
(creating the object files is a step in compilation)
A bunch of object files grouped together: library.
Kálmán, Oravecz, Szigetvári
Programs
WXVYGXLIPPS[SVPH
_
MRXVITIXMXMSR'SYRX
GLEVWXVMRKFYJ?1%<-191C0-2)0)2+8,A
a
program text
MRGPYHI MSWXVIEQ"
MRGPYHI WWXVIEQ"
JPSEXWUYEVIJPSEXRYQFIV
_
VIXYVRRYQFIVRYQFIV
a
QEMRMRXEVKGGLEVEVKZ
_
JPSEX\
WXHMWXVMRKWXVIEQMWEVKZ?A
MW""\
WXHGSYX WUYEVI\ WXHIRHP
a
Kálmán, Oravecz, Szigetvári
Programs
Programming style
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Program files
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Programming style
A simple program
Program files
Structure of a program
a header file
MRGPYHI WXHMSL"
HIJMRI1%<-191C0-2)0)2+8,
Types of program files
A famous type is called a header. It contains useful stuff
(but usually no algorithm).
It is kept separate because many other files may include it.
(remember the first line of our C and C++ programs?)
#include <stdio.h>
#include <iostream>
“Normal” files containing algorithms
Previously compiled parts of programs that can be used by
others, without recompilation: object files
(creating the object files is a step in compilation)
A bunch of object files grouped together: library.
Kálmán, Oravecz, Szigetvári
Introduction
Meet your first program
WXVYGXLIPPS[SVPH
_
MRXVITIXMXMSR'SYRX
GLEVWXVMRKFYJ?1%<-191C0-2)0)2+8,A
a
program text
MRGPYHI MSWXVIEQ"
MRGPYHI WWXVIEQ"
JPSEXWUYEVIJPSEXRYQFIV
_
VIXYVRRYQFIVRYQFIV
a
QEMRMRXEVKGGLEVEVKZ
_
JPSEX\
WXHMWXVMRKWXVIEQMWEVKZ?A
MW""\
WXHGSYX WUYEVI\ WXHIRHP
a
Kálmán, Oravecz, Szigetvári
Programs
Elements of programming
Programming style
A simple program
Program files
Introduction
Meet your first program
Programs
Elements of programming
Program files
Structure of a program
object files in a directory
Types of program files
A famous type is called a header. It contains useful stuff
(but usually no algorithm).
It is kept separate because many other files may include it.
(remember the first line of our C and C++ programs?)
#include <stdio.h>
#include <iostream>
FMXWXVIEQSMHXEKSTVIWIXWSVIWIVZSMVSYXMPS
IRGSHIVSPEQISTW]QSHIPSWIXCKIXSZFVUYERXM^IS
JJXSQTKPMFCMRXIVJEGISUYERXM^ISXEFPIWS:FV8EKS
KEMRCEREP]WMWSRI[QHGXSUYERXM^ICTZXSXEOILMVSSZIVWMSRS
libraries in a directory
PMFJKPV\CHQWSPMFJKPV\CTTWSPMF+0WS
PMFJKPV\CKEQQEWSPMFJKPV\CXZSYXWS
“Normal” files containing algorithms
Previously compiled parts of programs that can be used by
others, without recompilation: object files
(creating the object files is a step in compilation)
A bunch of object files grouped together: library.
Kálmán, Oravecz, Szigetvári
Programs
Kálmán, Oravecz, Szigetvári
Programs
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Program files
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Program files
Structure of a program
Types of program files
A famous type is called a header. It contains useful stuff
(but usually no algorithm).
It is kept separate because many other files may include it.
(remember the first line of our C and C++ programs?)
#include <stdio.h>
#include <iostream>
object files in a directory
FMXWXVIEQSMHXEKSTVIWIXWSVIWIVZSMVSYXMPS
IRGSHIVSPEQISTW]QSHIPSWIXCKIXSZFVUYERXM^IS
JJXSQTKPMFCMRXIVJEGISUYERXM^ISXEFPIWS:FV8EKS
KEMRCEREP]WMWSRI[QHGXSUYERXM^ICTZXSXEOILMVSSZIVWMSRS
libraries in a directory
PMFJKPV\CHQWSPMFJKPV\CTTWSPMF+0WS
PMFJKPV\CKEQQEWSPMFJKPV\CXZSYXWS
“Normal” files containing algorithms
Previously compiled parts of programs that can be used by
others, without recompilation: object files
(creating the object files is a step in compilation)
A bunch of object files grouped together: library.
Kálmán, Oravecz, Szigetvári
Introduction
Meet your first program
Kálmán, Oravecz, Szigetvári
Programs
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
Programs
Elements of programming
Structure of a program
Structure of a program
Structure of a program
Structure of a program
Programming style
preamble: what to include (import, use, require etc.)
import sys
definition of functions (procedures, subroutines etc.)
import sys
def intersect( seq1, seq2 ):
result = []
for x in seq1:
if x in seq2:
result.append(x)
return result
def intersect( seq1, seq2 ):
result = []
for x in seq1:
if x in seq2:
result.append(x)
return result
#-----------------
#-----------------
first=sys.argv[1]
second=sys.argv[2]
print "In common: ", intersect(first,second)
first=sys.argv[1]
second=sys.argv[2]
print "In common: ", intersect(first,second)
Kálmán, Oravecz, Szigetvári
Programs
Kálmán, Oravecz, Szigetvári
Programs
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
Structure of a program
Structure of a program
Structure of a program
Function (procedure etc.) definitions
the main part (special function in some p.l.)
import sys
Function declaration keyword: Function (procedure etc.)
name: Parameter (argument etc.) name(s): Function
(procedure etc.) body: Return statement: A loop: A
condition/test: An expression: A local variable:
def intersect( seq1, seq2 ):
result = []
for x in seq1:
if x in seq2:
result.append(x)
return result
def intersect( seq1, seq2 ):
result = []
for x in seq1:
if x in seq2:
result.append(x)
return result
#----------------first=sys.argv[1]
second=sys.argv[2]
print "In common: ", intersect(first,second)
Kálmán, Oravecz, Szigetvári
Introduction
Meet your first program
Kálmán, Oravecz, Szigetvári
Programs
Elements of programming
Programming style
A simple program
Introduction
Meet your first program
Structure of a program
Structure of a program
The main program
Sample run
Set first and second to parameters after program name:
Call (invoke) the intersect()function: Print the result of
intersect()with some text:
first=sys.argv[1]
second=sys.argv[2]
print "In common: ", intersect(first,second)
Kálmán, Oravecz, Szigetvári
Programs
A simple program
Programs
Elements of programming
Programming style
$ python intersect.py "Monty" "Python"
In common: [’o’, ’n’, ’t’, ’y’]
$ python intersect.py "ex-parrot" "dead"
In common: [’e’, ’a’]
Kálmán, Oravecz, Szigetvári
Programs
A simple program
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Programming style
):
Start with empty list
Scan seq1
Common item?
Add to the end
function/procedure: max 1 screen long
use many files (unless procedures really belong together)
(easier to modify later on)
Maximize abstraction.
What is abstraction?
similar procedures must be eliminated
replace similar procedures with one general procedure
Kálmán, Oravecz, Szigetvári
Programs
with parameters
Meet
your first program
Elements
of programming
A simple program
Would
you write a
programme
for 52 ?Programming
Clearly,style
a
Analysis
function digitalisOra()
{
// create a Date object and store it into ‘ido’:
var ido = new Date();
// this will be the string that we write out:
var kiirando;
// get hour of the day from Date object, and
// store it into a local variable:
var ora = ido.getHours();
// check whether AM or PM:
if ( ora > 12 ) // condition: bigger than noon
// block to be executed if condition holds:
{
ora -= 12; // let ora be 12 less (e.g., 17 -> 5)
kiirando = "Délután "; // set string to be printed
}
else
// statement to be executed if condition false:
kiirando = "Délel˝
ott ";
var perc = ido.getMinutes();
var masodperc = ido.getSeconds();
// append hours and the string to result:
kiirando += ora + " óra ";
kiirando += perc + " perc ";
kiirando += masodperc + " másodperc van";
// return the string:
return( kiirando );
}
Kálmán, Oravecz, Szigetvári
Meet your first program
Elements of programming
Programming style
A simple program
Programming style
A simple program
A simple program
Comment everything.
def intersect( seq1, seq2
result = []
#
for x in seq1:
#
if x in seq2:
#
result.append(x) #
return result
Separate everything.
Introduction
Introduction
Programs
(the programming language is JavaScript)
JYRGXMSRHMKMXEPMW3VE
_
ZEVMHS!RI[(EXI
ZEVOMMVERHS
ZEVSVE!MHSKIX,SYVW
MJSVE"
_
SVE!
OMMVERHS!(¬PYX¤R
a
IPWI
OMMVERHS!(¬PIPXX
ZEVTIVG!MHSKIX1MRYXIW
ZEVQEWSHTIVG!MHSKIX7IGSRHW
OMMVERHS!SVE¶VE
OMMVERHS!TIVGTIVG
OMMVERHS!QEWSHTIVGQ¤WSHTIVGZER
VIXYVROMMVERHS
a
Kálmán, Oravecz, Szigetvári
Introduction
Meet your first program
Programs
Elements of programming
function digitalisOra()
{
// create a Date object and store it into ‘ido’:
var ido = new Date();
// this will be the string that we write out:
var kiirando;
// get hour of the day from Date object, and
// store it into a local variable:
var ora = ido.getHours();
// check whether AM or PM:
if ( ora > 12 ) // condition: bigger than noon
// block to be executed if condition holds:
{
ora -= 12; // let ora be 12 less (e.g., 17 -> 5)
kiirando = "Délután "; // set string to be printed
}
else
// statement to be executed if condition false:
kiirando = "Délel˝
ott ";
var perc = ido.getMinutes(); // get time
var masodperc = ido.getSeconds();
// append time and the string to result:
kiirando += ora + " óra ";
kiirando += perc + " perc ";
kiirando += masodperc + " másodperc van";
// return the string:
return( kiirando );
}
Kálmán, Oravecz, Szigetvári
Programs
Introduction
Meet your first program
Elements of programming
Programming style
A simple program
Building it into HTML
Include the definition:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="application/x-javascript">
<script type="text/javascript" src="ora.js"></script>
</head>
<body>
<h1>
<script type="application/x-javascript">
<!-document.write( digitalisOra() );
// -->
</script>
</h1>
</body>
</html>
Invoke our function using document.write():
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="application/x-javascript">
<script type="text/javascript" src="ora.js"></script>
Kálmán, Oravecz, Szigetvári
Programs
</head>