Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

1.4 Fundamentals of Programming

The document provides an overview of the fundamentals of programming, focusing on the functional programming paradigm, which treats computation as the evaluation of mathematical functions and avoids mutable data. It discusses the historical context, key characteristics, and advantages of functional programming, including concepts like higher-order functions, pure functions, recursion, and type inference. Additionally, it highlights the application of functional programming in both academic and industrial settings, as well as its influence on non-functional programming languages.

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1.4 Fundamentals of Programming

The document provides an overview of the fundamentals of programming, focusing on the functional programming paradigm, which treats computation as the evaluation of mathematical functions and avoids mutable data. It discusses the historical context, key characteristics, and advantages of functional programming, including concepts like higher-order functions, pure functions, recursion, and type inference. Additionally, it highlights the application of functional programming in both academic and industrial settings, as well as its influence on non-functional programming languages.

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

UNIT I

Fundamentals of Programming

1
Unit I Contents (06 Hours)

• Importance of Studying Programming Languages, History of Programming


Languages, Impact of Programming Paradigms, Role of Programming
Languages, Programming Environments. Impact of Machine Architectures:
The operation of a computer, Virtual Computers and Binding Times.
• Programming paradigms- Introduction to programming paradigms,
Introduction to four main Programming paradigms- procedural, object
oriented, functional, and logic and rule based.
Functional Programming programming
paradigm
Functional Programming Paradigm

• Functional programming is a programming paradigm that treats computation


as the evaluation of mathematical functions and avoids state changes and
mutable data.

• It emphasizes the application of functions, in contrast to the imperative


programming style, which emphasizes changes in state.

• Programs written using the functional programming paradigm are much


more easily representable using mathematical concepts, and thus it is much
more easy to mathematically reason about functional programs than it is to
reason about programs written in any other paradigm.
Functional Programming

• Functional programming has its roots in the lambda calculus, a formal system
developed in the 1930s to investigate function definition, function application,
and recursion. Many functional programming languages can be viewed as
elaborations on the lambda calculus.

• LISP was the first operational functional programming language.

• Up to this day, functional programming has not been very popular except for a
restricted number of application areas, such as artificial intelligence.

• John Backus presented the FP programming language in his 1977 Turing Award
lecture "Can Programming Be Liberated From the von Neumann Style? A
Functional Style and its Algebra of Programs".
Functional Programming
• Functional programming languages, especially purely functional ones, have
largely been emphasized in academia rather than in commercial software
development.
• However, prominent functional programming languages such as Scheme,
Erlang, Objective Caml, and Haskell have been used in industrial and
commercial applications by a wide variety of organizations.
• Functional programming also finds use in industry through domain-specific
programming languages like R (statistics), Mathematica (symbolic math), J
and K (financial analysis), F# in Microsoft .NET and XSLT (XML).
• Widespread declarative domain-specific languages like SQL and Lex/Yacc,
use some elements of functional programming, especially in eschewing
mutable values. Spreadsheets can also be viewed as functional
programming languages.
Functional Programming
• In practice, the difference between a mathematical function and the notion
of a "function" used in imperative programming is that imperative functions
can have side effects, changing the value of already calculated variables.
• Because of this they lack referential transparency, i.e. the same language
expression can result in different values at different times depending on the
state of the executing program.
• Conversely, in functional code, the output value of a function depends only
on the arguments that are input to the function, so calling a function f twice
with the same value for an argument x will produce the same result f(x) both
times.
• Eliminating side-effects can make it much easier to understand and predict
the behavior of a program, which is one of the key motivations for the
development of functional programming.
Functional Programming: Higher-Order
Functions
• Most functional programming languages use higher-order functions, which
are functions that can either take other functions as arguments or return
functions as results.
• The differential operator d/dx that produces the derivative of a function f is an
example of this in calculus.
• Higher-order functions are closely related to functions as first-class citizen,
in that higher-order functions and first-class functions both allow functions as
arguments and results of other functions.
• The distinction between the two is subtle: "higher-order" describes a
mathematical concept of functions that operate on other functions, while "first-
class" is a computer science term that describes programming language
entities that have no restriction on their use (thus first-class functions can
appear anywhere in the program that other first-class entities like numbers
can, including as arguments to other functions and as their return values).
Functional Programming:
Pure Functions
• If the entire language does not allow side-effects, then any evaluation strategy
can be used; this gives the compiler freedom to reorder or combine the
evaluation of expressions in a program. This allows for much more freedom in
optimizing the evaluation.
• The notion of pure function is central to code optimization in compilers, even
for procedural programming languages.
• While most compilers for imperative programming languages can detect pure
functions, and perform common-subexpression elimination for pure function
calls, they cannot always do this for pre-compiled libraries, which generally do
not expose this information, thus preventing optimizations that involve those
external functions.
• Some compilers, such as gcc, add extra keywords for a programmer to explicitly
mark external functions as pure, to enable such optimizations. Fortran 95 allows
functions to be designated "pure" in order to allow such optimizations.
Functional Programming:
Recursion
• Iteration in functional languages is usually accomplished via recursion.
• Recursion may require maintaining a stack, and thus may lead to inefficient
memory consumption, but tail recursion can be recognized and optimized by a
compiler into the same code used to implement iteration in imperative languages.
• The Scheme programming language standard requires implementations to
recognize and optimize tail recursion.
• Tail recursion optimization can be implemented by transforming the program into
continuation passing style during compilation, among other approaches.
• Common patterns of recursion can be factored out using higher order functions,
catamorphisms and anamorphisms, which "folds" and "unfolds" a recursive
function call nest.
• Using such advanced techniques, recursion can be implemented in an efficient
manner in functional programming languages.
Functional Programming:
Eager vs. Lazy Evaluation
• Functional languages can be categorized by whether they use strict (eager) or
non-strict (lazy) evaluation, concepts that refer to how function arguments are
processed when an expression is being evaluated. Under strict evaluation, the
evaluation of any term containing a failing subterm will itself fail. For example, the
expression
print length([2+1, 3*2, 1/0, 5-4])
• will fail under eager evaluation because of the division by zero in the third element
of the list. Under lazy evaluation, the length function will return the value 4 (the
length of the list), since evaluating it will not attempt to evaluate the terms making
up the list.
• Eager evaluation fully evaluates function arguments before invoking the function.
Lazy evaluation does not evaluate function arguments unless their values are
required to evaluate the function call itself.
• The usual implementation strategy for lazy evaluation in functional languages is
graph reduction. Lazy evaluation is used by default in several pure functional
languages, including Miranda, Clean and Haskell.
Functional Programming:
Type Inference
• Especially since the development of Hindley–Milner type inference in the
1970s, functional programming languages have tended to use typed lambda
calculus, as opposed to the untyped lambda calculus used in Lisp and its
variants (such as Scheme).
• Type inference, or implicit typing, refers to the ability to deduce
automatically the type of the values manipulated by a program. It is a
feature present in some strongly statically typed languages.
• The presence of strong compile-time type checking makes programs more
reliable, while type inference frees the programmer from the need to
manually declare types to the compiler.
• Type inference is often characteristic of — but not limited to — functional
programming languages in general. Many imperative programming
languages have adopted type inference in order to improve type safety.
Functional Programming:
In Non-functional Languages
• It is possible to employ a functional style of programming in languages that
are not traditionally considered functional languages.
• Some non-functional languages have borrowed features such as higher-
order functions, and list comprehensions from functional programming
languages. This makes it easier to adopt a functional style when using these
languages.
• Functional constructs such as higher-order functions and lazy lists can be
obtained in C++ via libraries, such as in FC++.
• In C, function pointers can be used to get some of the effects of higher-order
functions.
• Many object-oriented design patterns are expressible in functional
programming terms: for example, the Strategy pattern dictates use of a
higher-order function, and the Visitor pattern roughly corresponds to a
catamorphism, or fold.
Prof. S. N. Shelke
(Assistant Professor)
Department of Computer Engineering
Sinhgad Academy of Engineering,
Kondhwa, Pune

You might also like