# Sebesta ch00 AIProgIntro-2020Dec - Rev1
# Sebesta ch00 AIProgIntro-2020Dec - Rev1
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
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
• 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.