





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to functional programming languages, focusing on the functional programming paradigm and mathematical functions. It covers the concept of functions, lambda expressions, higher order functions, and functional forms such as function composition and apply-to-all. Examples of functional programming languages like lisp, scheme, common lisp, and ml are discussed.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Functional Programming Languages
The functional programming paradigm, which is based on mathematical functions, is the design basis for one of the most important non imperative styles of languages. This style of programming is supported by functional or applicative, programming languages.
LISP, Scheme, COMMON LISP and ML are some of the programming languages coming under functional programming language.
Mathematical functions
E.g.
f( x) ≡ x + g( x) ≡ 3 * x then h is defined as h(x) ≡ f(g(x)), or h(x) ≡ (3 *x) +
Construction is a functional form that takes a list of function as parameters. When applied to an argument, a construction applies each of its functional parameters to that argument and collects the results in a list or sequence. E.g. g(x) ≡ x * x h(x) ≡ 2 * x i(x) ≡ x / 2 then g, h, i yields (16, 8, 2)
Apply-to-all is a functional form that takes a single function as a parameter. If applied to a list of arguments, apply-to-all applies its functional parameter to each of the values in the list argument and collects the results in a list or sequence. E.g. h(x) ≡ x * x then α(h, (2, 3, 4)) yields ( 4, 9, 16) Fundamentals of functional programming languages The objective of the design of a functional programming language is to mimic mathematical functions to greatest extent possible. In an imperative language, an expression is evaluated and the result is stored in a memory location which is represented as a variable in a program. A purely functional programming language does not use variables or assignment statements. This frees programmer from concerns about memory cells of computer on which is executed.
LISP
As a small language with simple syntax and semantics, Scheme is well suited to educational applications, such as courses in functional programming and also general introductions to programming. Primitive functions Names in scheme can consist of letters, digits and special characters except parentheses. They are case insensitive but must not begin with a digit. Scheme includes primitive functions for basic arithmetic operations. These are +, -, , and /. and + can have zero or more parameters. / and – can have two or more parameters. EXPRESSION VALUE 42 42 (* 3 7) 21 (+ 5 7 8) 20 (- 5 6) - (- 15 7 2) 6 (- 24 ( * 4 3)) 12
SQRT returns the square root of its numeric parameter, if the parameter value is not negative.
EVAL is the basis of all function evaluation in scheme, whether primitive function or otherwise. It is called to handle the evaluate part of the read-evaluate-write action of the scheme interpreter.
QUOTE is to find a literal or atom without evaluating a list. E.g. (QUOTE A) returns A (QUOTE (A B C )) returns A B C
CAR :- returns the first element of the given list E.g. (CAR ‘(A B C)) returns A (CAR ‘( (A B) C D)) returns (A B)
(CAR ‘A) is an error (A is not a list) (CAR ‘(A)) returns A (CAR ‘( )) is an error CDR ;- returns remainder of the given list. E.g. (CAR ‘(A B C)) returns B C (CAR ‘( (A B) C D)) returns (C D)
CONS:- is a primitive list constructor. It builds list from its two arguments, the first of which can be either atom or a list; the second is usually a list. E.g. (CONS ‘A ‘( ) ) returns (A) CONS ‘A ‘(B C) returns ( A B C) LIST :- is a function that constructs list from a variable number of parameters. It is a shorthand version of nested CONS functions, as illustrated in (LIST ‘apple ‘orange ‘grape) Which is equivalent to (CONS ‘apple (CONS ‘orange(CONS ‘grape ‘( ) )))
Three important predicate functions among schemes primitive functions are EQ? NULL? LIST?
Two Boolean values in scheme are #T & #F
E.g. for EQ (EQ? ‘A ‘A) returns #T (EQ? ‘A ‘B) returns ( ) means false (EQ? ‘A ‘ (A B)) returns ( )
E.g. for LIST (LIST? ‘(X Y) ) returns #T
DEFINE serves for two fundamental needs of scheme programming, to bind a name to a value and bind a name to lambda expression. (DEFINE symbol expression) E.g. (DEFINE pi 3.14159) (DEFINE two_pi (* 2 pi)) When DEFINE is used to bind with a lambda expression the word lambda is removed. (DEFINE (function_name parameters) Body ) E.g. (DEFINE (square number) (* number number)) It can be used as (square 5) which returns 25. E.g. (DEFINE (factorial n) (IF (= n 0) 1 ( * n (factorial (- n 1)) )) COND is special multiple selector form for scheme. The general form for COND is (COND (Predicate_1 expression {expression}) (Predicate_2 expression {expression}) …… (Predicate_n expression {expression}) (ELSE expression {expression}) )
COMMON LISP COMMON LISP was created in an effort to combine features of several early 1980s dialects of LISP, including scheme, into a single language.
The list of features of COMMON LISP is long:
COMMON LISP, along with most dialect of LISP except scheme, includes a function named PROG that allows statement sequencing, as is common in imperative languages. Labels and the two functions GO and RETURN, are included to provide iteration control. GO is used to transfer control to label within scope of PROG.RETURN is a means of exiting the PROG. The general form of PROG is (PROG(local variables) expression_ ….. expression_n ) ML ML (Milner et al.. 1990) is a static scoped functional programming language, like scheme. It uses a syntax that is more similar to that of pascal than that of LISP. It has type declarations, uses type inferencing, which means that variables need not be declared, and it is strongly typed. ML has exception handling and a module facility for implementing abstract data types. In ML names can be bound to values with value declarations statements of the form val new_name = expression; E.g. val distance = time * speed The normal use of val is in a let expression l et val new_name = expression_1 in expression_2 end E.g. let