{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programming Paradigms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **imperative programming**: modifying mutable variables, using assignments, and control\n", "structures such as if-then-else, loops, continue, return, etc\n", "inspired by Von Neumann architecture of computers.\n", "\n", "* **functional programming**\n", "programming without mutable variables, assignments, loops,\n", "other imperative control structures; programming with functions;\n", "functions become values that are produced, consumed, and composed;\n", "functions can be passed as parameters and returned as values.\n", "\n", "* **logic programming**\n", "programming in logic; use logical deductions to run a program; programs\n", "are a set of logical rules and facts; solutions focus on “what” aspect of\n", "the problem and let the system figure out “how” to solve them.\n", "\n", "Orthogonal to\n", "\n", "* **Object-oriented programming**\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functional Programming in Scala" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scala Basics" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres0\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m232\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "87 + 145" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36msize\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m2\u001b[39m" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val size = 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres2\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m10\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5*size" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
pi: Double = 3.14
\n",
"radius: Double = 21.5
\n",
"