Software Design and Development

Home > Software Design and Development > Options > Evolution of Programming Languages > Programming Paradigms

Functional Paradigm

The functional paradigm is about having a program which specifies what has to be computed, rather than how the computation is to be performed.

It is harder to implement efficient code using functional languages than it is with imperative and object oriented languages. Even without an efficient implementation, functional languages can still be useful.

Functional programming languages – such as LISP, Miranda, Scheme and Haskell – consist of functions. A function takes zero or more parameters (arguments) and computes a result which depends only upon the arguments.

; take a number and return the square of it
(define (square num)
    (* num num)
)

It should be noted that all operations are written in pre-fix notation (operator at the front of the operands, e.g. * num num) rather than in-fix (the operator between the operands, e.g. num * num).

Recursive functions play an important role in functional languages. Recursion is used instead of repetition, because the only control structure in functional languages is selection.

; find the factorial of a number (i.e. N! = 1*2*3* … *N)
; remember that factorial (0) is 1
(define (factorial n)
    (if (eq? n 0) 1
        (* n (factorial (- n 1)))
    )
)

In a functional language there are no global variables. All variables are passed as parameters into a function and therefore have only a local scope to the current call of a function.

As there are no variables other than arguments, the return value from a function must either be passed to another function or be the output of the program.

In functional languages, the list is the most important data structure. A list can consist of zero or more elements, and lists can be nested.

(list 3 4 56) a list of three numbers
(list "a" "j" "o" "p") a list of four characters
(list ) an empty list
(list (list 2 3) (list 5 6)) lists within a list

Functions can be written to deal with lists

; finds the length of a list. The empty list has 0 elements in it. l is a list.
(define (length l)
    (if (empty? l) 0
        (+ 1 (length (rest l)))
    )
)



Neals logo | Copyright | Disclaimer | Contact Us | Help