Download Praktikum Bahasa Pemrograman (Functional Programming)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Common Lisp wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Lisp (programming language) wikipedia , lookup

Transcript
Praktikum Bahasa Pemrograman
Departemen Ilmu Komputer FMIPA IPB 2012
Revised : 15 November 2009
Pengelompokan Bahasa Pemrograman
• Bahasa pemrograman dapat dikelompokkan berdasarkan struktur
pemrogramannya, terutama dalam mengimplementasikan suatu
ekspresi.
– Jika setiap ekspresi selalu dinyatakan dalam bentuk fungsi, disebut
functional programming, mis. LISP, Scheme dan HASKELL.
– Jika ekspresi dinyatakan dan ditulis secara deskriptif yang diproses
secara logika disebut logic programming, mis. Prolog.
– Bahasa pemrograman diproses oleh komputer berdasarkan aliran
kontrolnya. Jika aliran kontrol dinyatakan secara prosedural disebut
programming with assignment, mis. Basic, Pascal, C, Modula-2,
Fortran, dan sebagainya.
– Jika aliran kontrol tidak bersifat unik disebut concurrency
programming, mis. Ada dan Euclid.
– Object oriented programming, data dan prosedur dikemas ke dalam
satu obyek, mis. C++, smalltalk, dan JAVA. Bahasa pemrograman ini
berkembang hingga muncul teknik pemrograman yang disebut sebagai
visual programming, yang merupakan metode lain untuk
mengimplementasikan pemrograman berorientasi obyek.
[email protected]
2
Functional Programming (Scheme)
Departemen Ilmu Komputer FMIPA IPB 2009
Scheme
Departemen Ilmu Komputer FMIPA IPB 2008
Instalasi
• Download Dr Scheme di http://download.plt-scheme.org/
• Instal program tersebut di komputer (Ver. 4.1.3)
• Konfigurasi:
– Choosing language
language > choose language > Module > OK
• Program LISP
– Berbasis Windows, Mac, Linux, Solaris
– PLT Scheme > DrScheme
– Semua perintah diantara “( .... )” dan termasuk dalam prefix instruction
[email protected]
5
• PLT Scheme’s two main tools are
– MzScheme, the core compiler, interpreter, and run-time system; and
– DrScheme, the programming environment (which runs on top of
MzScheme).
• If you’re using DrScheme, you’ll need to choose the proper
language, because DrScheme accommodates many different
variants of Scheme.
#lang scheme
[email protected]
6
DrScheme
Definitions window
• statements are entered all at once, and then
run all at the same time.
•To execute the contents of the Definitions
window, click the green Execute/Run button.
Any output from the statements goes into the
Interactions window.
Interactions window
• like a pocket calculator.
•You can use this window :
•to test short pieces of code, or
•to run functions that you define in the
Definitions window.
•The text you enter is displayed in black after
the > sign, and the program’s responses are
displayed in blue.
[email protected]
7
Definitions window
• Usually the Definitions window is used to define functions, which
can then be tested in the Interactions window.
• However, any statement, not just (define …) statements, can be
executed from the Definitions window
[email protected]
8
Definitions window :
(define (kuadrat x)
(* x x))
Instruction window :
(kuadrat 10)
[email protected]
9
Definitions window
• You must click the Execute/Run button each time you want to test
your work in the Interactions window.
• If you forget to do this, then DrScheme will give you a yellow
warning, and it will run with the old definitions.
[email protected]
10
Editor
• Save
– File > Save Definitions / Save
Definitions as
– File > Save other > Save Definition as
Text / Save Instructions, …
•
Debugging
– To test these programs, you must
create a series of test cases.
DrScheme provides the ability to
make sure that your test cases cover
all of your code. If a section of code is
run, then it turns black – otherwise, it
stays red.
test case
• CTRL+D : show/hide Definitions
• CTRL+E : show/hide Instructions
[email protected]
11
Test-case
• Incomplete test-case
complete test-case
test case
[email protected]
12
Syntax Checking
• to see visually the relationships between your functions and
variables by coloring them for you.
• You can do this by clicking the Check Syntax button at the top of
DrScheme window  your variables are colored so you can see
your code more easily. This makes it much easier to navigate very
large scheme files.
[email protected]
13
Stepper
• a very powerful diagnostic tool
• The stepper lets you drill down into your program, examining each
expression as it is evaluated by the DrScheme engine.
• To step through your program, click the Step button.
[email protected]
14
#1 – PLT Scheme
Departemen Ilmu Komputer FMIPA IPB 2008
Interacting with Scheme
• A number by itself is an expression, and the answer is just the
number:
> 5
5
• A string is also an expression that evaluates to itself. A string is
written with double quotes at the start and end of the string:
> "hello world"
"hello world"
• Scheme uses parentheses to wrap larger expressions – almost any
kind of expression, other than simple constants.
– open parenthesis, function name, argument expression, and closing
parenthesis. The following expression calls the built-in function
substring with the arguments "hello world", 0, and 5:
> (substring "hello world" 0 5)
"hello"
[email protected]
16
Definitions and Interactions
• You can define your own functions that work like substring by using
the define form, like this:
Definitions:
(define (piece str)
(substring str 0 5))
Interactions:
> (piece “makan malam hari ini dengan menu apa")
“makan"
[email protected]
17
Creating executables
• Definitions:
(define (piece str)
(substring str 0 5))
(piece "malam ini makan menu apa")
• Untuk membuat executable file :
– In DrScheme, you can select the Scheme|Create Executable... menu
item.
– Tuliskan nama file dan tentukan lokasi penyimpanan dari executable file
– Hasil : nama_file.zip  berisi folder lib dan .exe
[email protected]
18
#1 - Basic LISP Primitives
Departemen Ilmu Komputer FMIPA IPB 2008
GCLISP
• LISP (List Processing) digunakan untuk Symbol Manipulation
• ATOM : fundamental things formed from bits are wordlike object
LIST : groups of atom from sentence like obj.
• * : prompt pada LISP
; : comment
penulisan symbolic expression pada LISP ada di antara (...)
• Penulisan kode bisa menggunakan UPPER /LOWER CASE, tetapi
LISP akan merespon (memberikan jawaban) dalam UPPER CASE
• (+ a b)  prefix notation
+ : procedures
a dan b : argumen / elemen
• Prosedur : primitive, user defined procedure
kumpulan prosedur  program
[email protected]
20
ATOM and LIST
• Atom : indivisible things which have obvious meaning (Numeric and
Symbolic Expression)
– Symbolic atom (symbol) : FIRST, +, FOO
– Numeric atom (number) : 27, 3.14
• List : consist of a left parenthesis, followed by zero or more atoms or
list, followed by a right parenthesis ( [zero/atoms/list] )
• ATOM and LIST  Symbolic Expression (expression)
Atom
Number
Symbol
• Expression
List
[email protected]
21
Problem Atom and List
• Identifikasi latihan soal di bawah ini, apakah sebagai atom, list atau
bukan keduanya
* ATOM
* (THIS IS AN ATOM)
* (THIS IS AN EXPRESSION)
* ((A B) (C D)) 3 (3)
* (LIST 3)
* (/ (+ 3 1) (- 3 1))
* )(
* ( ( () ) )
* ( () () )
* ((())
* ())(
* ((ABC
[email protected]
22