Friday, March 5, 2010

Functional Programming

The main principles of functional programming (FP) is that the value of an expression only depends on the value of sub-expression. Value expressions like a + b only depends to the value of a and b values.This principle eliminates the side effects in expressions so that FP can be called programming without assignment. The absence these side effects, an expression has the same value each time evaluated.
Another characteristic of functional languages is that users do not need to worry about data storage management because:

• management of storage (data) implicit.
• operations are built-in for the allocation of data storage in accordance
purposes.
• storage (storage) which would cause failure in accessing
not automatically allocated.
• the absence of an explicit code to make the program dealokasi simpler and
more concise.

The consequence of this approach is that the implementation of this language should be made garbage collection, to reclaim the storage that has an accesed storage or not.

Finally, FP treats functions as "first-class citizen":
• Functions are first-class values.
• The functions have the same status with the values of others.
• An expression may be a function, the function can be pass as an argument and
functions can be used in the data structure.

WHY LISP?
Lisp has been widespread and is one of the preferred language for applications such as artificial intelligence. Functional language in general, and LISP in particular, play a role important in the definition of language. A language definition should be written into the notations, called meta-language (meta-language) or language that is defined (defining language), and the language tends to be defined functionally. Deep
in fact, the first implementation of LISP began, when LISP is used to defines himself.

FP basic concepts derived from LISP. LISP was designed by John McCarthy in 1958,
LISP is probably the second oldest language, after fortran. Believed LISP as
the first provider of recursion, the main-class functions, garbage collection, and a definition

formal language (in LISP itself). LISP implementations are also pushing in the direction integrated programming environment, which combines an editor, interpreter and debugger.

With these advantages, why is not everyone using LISP?
• First, LISP has a unique syntax, LISP has many parentheses ().
• Second, LISP programs do not type; LISP does not connect type
expression.
• Third, implementation of LISP in the past is inefficient. At the beginning of the year 1960 --
an, LISP is very slow for numerical calculations.
PREFIX EXPRESSION by parentheses
Notation used in LISP is a prefix notation with parentheses.
Arithmetic such as 5 * 7 is written as
(* 5 7)
35
Expressions in LISP syntax is
(E1 E2 E3 ... Ek)
Here, the expression of E1 expressed an operator that is applied to the values
E2 ,..., Ek. The order of evaluation of expression E1, E2, ..., Ek are not specified; However,
all sub-expression value will be calculated before the value of E1 is applied to its operand.

Arithmetic expressions with several operators can be translated into LISP
with the following composition of sub-expression. Expression 4 +5 * 7 is the sum of 4 and 5 * 7:
(+ 4 (* 5 7))
39

Quoting
Quoting needed to treat expressions as data. Items quot
count for the item itself. Syntax in-amp will be explained as a symbol.
Symbol is an object with the spelling. Quoting is used to select whether the spelling
treated as a symbol or as a variable name.

The items can be quoted with 2 ways:
(quote )


FUNCTION DEFINITION
Here's one way to define the function:
(defun (function> ) )
For example
(defun (square x) (* x x))
square
(square 5)
25
Function is to connect the value of a quadratic function. Function and
take a variable and variables with variables multiplying itself.
Associate a function with the value of the function more clearly in the following syntax
(defun function> )
To use the syntax, the notation required for the function values. LISP
provides the following notation:
(lambda () )

for the value of nameless functions. Thus, the function with variables x and functions (* xx) written
(lambda(x) (* x x))
Squares can now be defined as
(defun square (lambda (x) (* x x)))
square
Function without a name can also be regarded as an operator in the expression
((lambda (x) (* x x)) 5)
25

Conditional
Boolean values true (true) and false (false) is written in a row T and NIL. Predicate
is an expression that will produce true or false when evaluated. Expression
conditional has 2 different forms:
(if P E1 E2): if P then E1 else E2
Another form is the form of sequential choices:
(COND (P1 E1); if P1 then E1
...
...
(Pk Ek); if the Oak Pk
(T Ek +1); others Ek +1

Conditional required for recursive functions, such as the following definition of factorial function:
(defun factorial (n)
(COND ((= n 0) 1)
(T (* n (factorial (- n 1)))
))

1 comment:

Unknown said...

A very well written article.In this article of Functional programming explains all the aspects like definition, little bit briefing, examples and advantages also.It fulfills all the necessary blanks.
digital signature certificate