Recommended Exercises and Readings Topic 1: Introduction From Haskell: The craft of functional programming (3 rd Ed.) Readings: Chapter 1 Chapter 2 1 2 What is a Programming Paradigm? Programming Paradigm: A style or way of programming Influences how you solve problems and implement solutions Languages include features from one or more paradigms Imperative Programming A program consists of a sequence of commands Commands describe how the computation takes place Step by step Modify state Most modern imperative languages are structured Conditionals, loops and subroutines (functions) instead of gotos Variables are generally local to a block or subroutine 3 4
Object Oriented Programming Program describes objects and the ways they interact Emphasizes objects instead of actions and data rather than logic Principal concern is sending messages to objects Objects respond to messages by performing operations Messages can have arguments Look like calling a subroutine Similar objects are abstracted into a class Similar classes grouped together into inheritance hierarchies Often include mechanisms for encapsulation and information hiding Functional Programming Focus is on functions and the application of functions to values Evaluation of expressions rather than running commands Functions can be Constructed as a program runs Passed as parameters to other functions Returned as results from functions Pure functional languages Do not include loops Functions are side effect free No (modifiable) global state 5 6 Logic Programming Program is a collection of facts and inference rules Identify goal Unification and backtracking find a value that satisfies the goal Occurs automatically programmer doesn t specify how to find the solution Human Language In written form Letters are used to form words Words are used to form phrases Phrases are used to form sentences Sentences are used to form paragraphs Paragraphs are used to form sections Sections are used to form chapters Chapters are used to form books 7 8
Human Language But Not every sequence of letters is an allowed word Not every combination of words is an allowed phrase Not every combination of phrases is an allowed sentence Syntax defines the structure of what is allowed Grammar describes syntax Humans attach meaning to words Semantics: The branch of linguistics and logic concerning meaning Ambiguities in Language Look at the bow! 9 10 Programming Languages Syntax: Rules that describe valid combinations of symbols and the structure of a program Semantics: Rules that describe the meaning of the statements Programming languages Communicate instructions to the machine Can be written by a person Minimize ambiguity May be defined by Specification Implementation So Many Programming Languages! 18 19
So Many Programming Languages! Almost all programming languages are Turing complete With enough time and memory, they can compute anything that is computable Why are there so many programming languages? Programming languages evolve Learn from predecessors New problems may be easier to solve with new languages Different tools for different jobs Trade offs between safety, speed and convenience Different people have different tastes and abilities Variety is beneficial! 20 Haskell A functional programming language Originally developed in the 1990s Successor to Miranda General purpose Pure Offers several interesting features Strong type system with type inference and type classes Lazy evaluation Pattern matching List comprehensions Can be interpreted or compiled Popular in academia, and has seen some industrial use 21 Getting Haskell Download it for free https://www.haskell.org/downloads I used the Haskell Platform installer Available for Windows, Linux and MacOS Probably more than what you really need You ll also need your favourite editor Writing our First Function To define a function in Haskell we need It s name It s type Types of parameters passed to the function Type of the value returned by the function Syntactic requirements: Function names must start with a lowercase letter Types start with uppercase letters Function name is separated from its type by :: 22 23
Functions: Definitions in Different Languages Define a function named alwaysfive which always returns the Int 5 Haskell Types Int Fixed Range 2 64 to 2 64 1 on my system Integer Arbitrarily large integers Convert to/from other numeric types with tointeger and frominteger Float Operation Addition Subtraction Multiplication Division (Truncated integer result) Remainder Round to closest integer Absolute value + * quot rem abs Number with both an integer and fractional part Convert to/from other numeric types with tointegral/fromintegral Operator / Function round 24 25 Haskell Types Functions: Adding Parameters Boolean True or False Operators: &&, and not Char Literals enclosed in single quotes String Literals enclosed in double quotes Equivalent to [Char] Char: 'a', String ['a'] or "a" Concatenate strings with ++ 26 Define a function that returns twice it s Int parameter 27
Functions: Additional Parameters Define a fused multiply add function which computes a * b + c, where a, b and c are all of type Float Calling Functions Brackets aren t needed for many function calls in Haskell Places where brackets are needed Negative numbers Grouping to avoid passing a function as one of the parameters 28 29 Summary Programming languages Four broad paradigms Some languages have features from multiple paradigms Provide a mechanism to communicate with the computer Haskell A functional programming language Function type declaration: name :: Type Function implementation: name args = body 30