0% found this document useful (0 votes)
12 views20 pages

# Sebesta ch00 AIProgIntro-2020Dec - Rev1

The document discusses functional programming languages and concepts. It covers the history and characteristics of functional programming like pure functions, recursion, and functional composition. It provides examples of functional programming in JavaScript and notes on using functional style in non-functional languages like Python, PHP, Java, C#.

Uploaded by

t2xryr5vrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views20 pages

# Sebesta ch00 AIProgIntro-2020Dec - Rev1

The document discusses functional programming languages and concepts. It covers the history and characteristics of functional programming like pure functions, recursion, and functional composition. It provides examples of functional programming in JavaScript and notes on using functional style in non-functional languages like Python, PHP, Java, C#.

Uploaded by

t2xryr5vrs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Virtual Hands-on workshop:

Artificial Intelligence Programming Languages


Hosted by Dr. Jey Veerasamy
UT Dallas CS Department

AI Programming Languages
by Dr. Richard Min
December 28-31, 2020 (M-Th) 10am-1pm
via MS Teams Webinar room link: https://bit.ly/aipl-dec20
AI Programming Languages
• Day1 – Lisp and Functional Programming
• Day2 – Prolog and Logic Programming
• Day3 – Answer Set Programming, JavaScript programming
• Day4 – Python, for AI, ML and Game Programming

MS Teams Webinar room link: https://bit.ly/aipl-dec20


Programming Languages
5 families (categories) of Programming Languages
1. Functional Programming Languages
2. Procedural Programming Languages
3. Logic Programming Languages
4. Object-Oriented Programming Languages
5. Answer Set Programming Language
Popular Programming Languages
Survey of Programming Languages
• In Demand (Job), Trend, Use, Research, Education, …
• A few surveys:
https://spectrum.ieee.org/static/interactive-the-top-programming-languages-2018
https://www.tiobe.com/tiobe-index/
https://w3techs.com/technologies/overview/programming_language
The Top Programming Languages 2018 - IEEE Spectrum
https://spectrum.ieee.org/static/interactive-the-top-programming-languages-2018
The Top Programming Languages 2018 - IEEE Spectrum
https://spectrum.ieee.org/static/interactive-the-top-programming-languages-2018
TIOBE Index for December 2020
https://www.tiobe.com/tiobe-index/
The TIOBE Programming Community index is an indicator of the popularity of programming languages. The index is
updated once a month. The ratings are based on the number of skilled engineers world-wide, courses and third
party vendors. Popular search engines such as Google, Bing, Yahoo!, Wikipedia, Amazon, YouTube and Baidu are
used to calculate the ratings. It is important to note that the TIOBE index is not about the best programming
language or the language in which most lines of code have been written.
Server-side programming languages for websites – 12/27/2020
https://w3techs.com/technologies/overview/programming_language

The following server-side


programming languages are
used by less than 0.1% of the
websites
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming

Some Characteristics
• Pure Functions
• Referential transparency
• Immutability
• Recursion
• First-class and higher-order
• Functional composition
In mathematics, function composition is an operation
that takes two functions f and g and produces a function
h such that h(x) = g(f(x))
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming

• Functional Programming is a programming paradigm


• programs are constructed by applying and composing functions.
• a declarative programming paradigm
• function definitions are trees of expressions that each return a value
• functions are treated as first-class objects
• programs in a declarative and composable style in a modular manner
• purely functional programming with no mutable or side-effect
• Referential transparency
• Based on the lambda calculus (Alonzo Church)
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming

• Today in industry and education: Common Lisp, Scheme, ML, Clojure, Lua,
Erlang, OCaml, Haskell, and F#.
• Functional programming is also key to some languages that have found
success in specific domains, like R in statistics, J, K and Q in financial
analysis, and XQuery/XSLT for XML.
• Domain-specific declarative languages like SQL and Lex/Yacc use some
elements of functional programming, such as not allowing mutable values.
• In addition, many other programming languages support programming in a
functional style or have implemented features from functional
programming, such as C++11, Java 8, JavaScript, Kotlin, Perl, PHP, Python,
and Scala.
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming

• The first functional programming language, LISP, was developed in the late
1959 for the IBM 700/7000 series of scientific computers by John McCarthy
while at Massachusetts Institute of Technology (MIT).
• LISP functions were defined using Church's lambda notation, extended with
a label construct to allow recursive functions.
• Lisp first introduced many paradigmatic features of functional
programming, though early Lisps were multi-paradigm languages, and
incorporated support for numerous programming styles as new paradigms
evolved.
• Later dialects, such as Scheme and Clojure, and offshoots such as Dylan
and Julia, sought to simplify and rationalize Lisp around a cleanly functional
core, while Common Lisp was designed to preserve and update the
paradigmatic features of the numerous older dialects it replaced.
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming
• The following two examples (written in JavaScript) achieve the same effect: each
does: (1) to begin an array by 10, (2) to multiply all even numbers, (3) to add
them all, and then (4) to store the final sum in the variable "result".
• Traditional Imperative Loop:
const numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let result = 0;
for (let i = 0; i < numList.length; i++) {
if (numList[i] % 2 === 0) {
result += numList[i] * 10;
}
}
• Functional Programming with higher-order functions:
const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.filter(n => n % 2 === 0)
.map(a => a * 10)
.reduce((a, b) => a + b);
Javascript – example 1
Reference http://www.w3schools.com/jsref/default.asp

<!DOCTYPE html>
<html>
<body>
<h1>What Can JavaScript Do?</h1>
<p id="demo">JavaScript can change HTML content.</p>

<button type="button"
onclick="document.getElementById('demo').innerHTML
= 'Hello JavaScript!'">
Click Me!
</button>

</body>
</html>
Javascript – example 1
Reference http://www.w3schools.com/jsref/default.asp

<!DOCTYPE html>
<html>
<body>
<h1>What Can JavaScript Do?</h1>
<p id="demo">JavaScript can change HTML content.</p>

<button type="button"
onclick="document.getElementById('demo').innerHTML
= 'Hello JavaScript!'">
Click Me!
</button>

</body>
</html>
Javascript – example 2
Reference http://www.w3schools.com/jsref/default.asp
<!DOCTYPE html>
<html> <body>
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<p>Click the light bulb to turn on/off the light.</p>
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
</body> </html>
Javascript – example 2
Reference http://www.w3schools.com/jsref/default.asp

<!DOCTYPE html>
<html> <body>
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<p>Click the light bulb to turn on/off the light.</p>
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
</body> </html>
Javascript – example 2
Reference http://www.w3schools.com/jsref/default.asp

<!DOCTYPE html>
<html> <body>
<h1>JavaScript Can Change Images</h1>
<img id="myImage" onclick="changeImage()"
src="pic_bulboff.gif" width="100" height="180">
<p>Click the light bulb to turn on/off the light.</p>
<script>
function changeImage() {
var image = document.getElementById('myImage');
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
</body> </html>
Functional Programming Languages
https://en.wikipedia.org/wiki/Functional_programming
Notes on Functional programming in non-functional languages
Source: https://en.wikipedia.org/wiki/Functional_programming

• It is possible to use a functional style of programming in languages that are not traditionally considered functional languages.
• For example, both D and Fortran 95 explicitly support pure functions.
• JavaScript, Lua and Python had first class functions from their inception.
• Python had support for "lambda", "map", "reduce", and "filter" in 1994, as well as closures in Python 2.2, though Python 3 relegated
"reduce" to the functools standard library module.
• First-class functions have been introduced into other mainstream languages such as PHP 5.3, Visual Basic 9, C# 3.0, C++11, and Kotlin.
• In PHP, anonymous classes, closures and lambdas are fully supported. Libraries and language extensions for immutable data structures are
being developed to aid programming in the functional style.
• In Java, anonymous classes can sometimes be used to simulate closures; however, anonymous classes are not always proper replacements
to closures because they have more limited capabilities.
• Java 8 supports lambda expressions as a replacement for some anonymous classes.[80]
• In C#, anonymous classes are not necessary, because closures and lambdas are fully supported. Libraries and language extensions for
immutable data structures are being developed to aid programming in the functional style in C#.
• Many object-oriented design patterns are expressible in functional programming terms: for example, the strategy pattern simply dictates use
of a higher-order function, and the visitor pattern roughly corresponds to a catamorphism, or fold.
• Similarly, the idea of immutable data from functional programming is often included in imperative programming languages, for example the
tuple in Python, which is an immutable array.

You might also like