Pattern Matching

Total Page:16

File Type:pdf, Size:1020Kb

Pattern Matching Functional Programming Steven Lau March 2015 before function programming... https://www.youtube.com/watch?v=92WHN-pAFCs Models of computation ● Turing machine ○ invented by Alan Turing in 1936 ● Lambda calculus ○ invented by Alonzo Church in 1930 ● more... Turing machine ● A machine operates on an infinite tape (memory) and execute a program stored ● It may ○ read a symbol ○ write a symbol ○ move to the left cell ○ move to the right cell ○ change the machine’s state ○ halt Turing machine Have some fun http://www.google.com/logos/2012/turing-doodle-static.html http://www.ioi2012.org/wp-content/uploads/2011/12/Odometer.pdf http://wcipeg.com/problems/desc/ioi1211 Turing machine incrementer state symbol action next_state _____ state 0 __1__ state 1 0 _ or 0 write 1 1 _10__ state 2 __1__ state 1 0 1 write 0 2 _10__ state 0 __1__ state 0 _00__ state 2 1 _ left 0 __0__ state 2 _00__ state 0 __0__ state 0 1 0 or 1 right 1 100__ state 1 _10__ state 1 2 0 left 0 100__ state 1 _10__ state 1 100__ state 1 _10__ state 1 100__ state 1 _10__ state 0 100__ state 0 _11__ state 1 101__ state 1 _11__ state 1 101__ state 1 _11__ state 0 101__ state 0 λ-calculus Beware! ● think mathematical, not C++/Pascal ● (parentheses) are for grouping ● variables cannot be mutated ○ x = 1 OK ○ x = 2 NO ○ x = x + 1 NO λ-calculus Simplification 1 of 2: ● Only anonymous functions are used ○ f(x) = x2+1 f(1) = 12+1 = 2 is written as ○ (λx.x2+1)(1) = 12+1 = 2 note that f = λx.x2+1 λ-calculus Simplification 2 of 2: ● Only unary functions are used ○ a binary function can be written as a unary function that return another unary function ○ (λ(x,y).x+y)(1,2) = 1+2 = 3 is written as [(λx.(λy.x+y))(1)](2) = [(λy.1+y)](2) = 1+2 = 3 ○ this technique is known as Currying Haskell Curry λ-calculus ● A lambda term has 3 forms: ○ x ○ λx.A ○ AB where x is a variable, A and B are lambda terms. Church-Turing thesis ● Conjectures that Turing machines can simulate all computers ● Generally believed to be true ● A programming language that can simulate a Turing machine is said to be Turing-complete ○ Any examples? ○ C, C++, Pascal, most programming languages ○ English, Cantonese ○ lambda calculus 2 branches ● Turing machine: imperative programming ● Lambda calculus: functional programming Functional Programming We all know function Constant function f(x) = 2 Identity function id(y) = y Quadratic function g(x) = x2+5 more new terms ● Anonymous function ● Currying ● Higher-order function ● Pure function ● Referential transparency ● Lazy Evaluation Higher-order function Function that ● takes a function as input or ● outputs a function Higher-order function Examples: ● Differentiation ○ D(λx.x2+3) = λx.2x ● std::sort (C++) ○ sort(a, a + N, cmp); ● Curried functions ○ (λx.(λy.x+y))(1) = λy.1+y ○ also known as partial application Pure function A function is pure if it, ● always evaluates the same result given the same argument(s) ● does not cause side effects (e.g. output) Pure function A function is pure if it, ● always evaluates the same result given the same argument(s) ● does not cause side effects (e.g. output) pure functions: sin, strlen, max impure functions: rand, printf Pure function A function is pure if it, ● always evaluates the same result given the same argument(s) ● does not cause side effects (e.g. output) These properties are also called "referential transparency” Referential transparency ● helps reasoning behaviour ● assists modifying codes ● allows optimizations such as ○ memoization ○ parallelism ○ lazy evaluation Referential transparency ● helps reasoning the behaviour ● assists modifying codes ● allows optimizations such as ○ memoization ○ parallelism ○ lazy evaluation Lazy evaluation 1000000000 let f(x) = x mod 10 10 g(x) = 3 h(x) = g(f(x))2+2g(4) - x Evaluate h(5). Good trainees: Oh, Big Mod. #include <cstdi... Better trainees: Observe that 5k mod 10 = 5 for k ≥ 1, … Lazy trainees: Zzz... more new terms ● Anonymous function ● Currying ● Higher-order function ● Pure function ● Referential transparency ● Lazy Evaluation Functional programming in C++? #include <functional> ● Anonymous function available in C++11 ● Higher-order function implemented as function pointer or function object ● Partial application only for binary functions ○ std::bind1st Functional programming in ... Haskell The root Haskell ● (\x -> x^2 + 1)(1) ● (\x -> (\y -> x + y))(1)(2) Remember λ-calculus? ● (λx.x2+1)(1) ● (λx.(λy.x+y))(1)(2) Haskell Basics ● Variables ○ value ○ list ○ tuple ○ lambda function ○ where ○ let in ● Functions ● Control Structures ● More on types Variables - value k :: Double k = 0.5 l :: Int l = 2147483647 m :: Integer m = 1230193729237469187264987702987109823741052873452698734659187345928743501976 myExamGrade = ('A' :: Char) Variables - list k :: [Int] -- a list of Ints k = [1,2,3,4] l :: [[Int]] -- a list of lists of Ints l = [[1,2],[],k,[1]] s :: [Char] s = ['a','b','c'] -- s = "abc" Variables - list k :: [Int] -- a list of Ints k = 1:2:3:4:[] l :: [[Int]] -- a list of lists of Ints l = (1:2:[]):[]:k:(1:[]):[] s :: [Char] s = 'a':'b':'c':[] -- s = "abc" Variables - list k :: [Int] k = 1:2:3:4:[] k' :: [Int] k' = -1:0:k -- k' = [-1,0,1,2,3,4] Variables - list k :: [Int] k = [1..4] -- k = [1,2,3,4] l :: [Int] l = [1,3..9] -- l = [1,3,5,7,9] m :: [Integer] m = [1..] -- infinite list begin = 'a' end = 'z' alphabet = [begin..end] -- alphabet = "abcdefghijklmnopqrstuvwxyz" Variables - list comprehension s = [2 * x | x <- [0..], x^2 > 3] -- s = [4,6,8,10,......] digits = [x * y | x <- [1,2,3], y <- [1,2,3,4]] -- digits = [1,2,3,4,2,4,6,8,3,6,9,12] Variables - tuple a :: ([Char], Int, Char) a = ("Hello", 567, 'k') b :: Int -- b is NOT a tuple b = (123) c :: () -- c is a tuple c = () Variables - function f :: Int -> Int f = \x -> x^2 + 2*x + 1 k :: Int k = f 4 g :: Char -> Char -> [Char] g = \a b -> [a, b] g' = \a -> (\b -> [a, b]) k' :: [Char] k' = g 'm' 'n' Variables - where k :: Int k = x^2 + 3 where x = 2 l :: Int l = f 2 3 + g 4 5 where f = \x y -> x + y g = \x y -> x * y Variables - let in k :: Int k = (let x = 2 in x^2 + 3) l :: Int l = let f = \x y -> x + y g = \x y -> x * y in f 2 3 + g 4 5 Haskell Basics ● Variables ● Functions ○ more than λ ○ partial application ○ operator ○ infix function ○ guard ○ pattern matching ○ function composition ● Control structures ● More on types Functions - more than λ k :: Int -- nullary function, aka constant k = 1 f :: Int -> Int -- unary function f x = x + 1 -- f = \x -> x + 1 g :: Int -> (Int -> Int) -- binary function g x y = x + y -- g = \x y -> x + y Functions - partial application plus :: Int -> Int -> Int plus x y = x + y plusOne :: Int -> Int plusOne x = plus 1 x plusOne' :: Int -> Int plusOne' = plus 1 Functions - operators k :: Int k = (+) 2 1 l :: Int l = (-) 2 1 m :: [Int] m = (:) 1 [2,3,4] Functions - operators -- partial application also works plusOne :: Int -> Int plusOne = (+) 1 plusOne' = (1 +) plusOne'' = (+ 1) divideTwo :: Double -> Double divideTwo = (/ 2) oneMinus = (-) 1 :: Int -> Int oneMinus' = (1 -) :: Int -> Int minusOne = (- 1) :: Int -- not partial application Functions - infix function minus :: Int -> (Int -> Int) minus x y = x - y k :: Int k = 1 `minus` 2 -- only works for binary function -- k = minus 1 2 *note the backtick used Functions - infix function minus :: Int -> (Int -> Int) minus x y = x - y oneMinus :: Int -> Int oneMinus = (1 `minus`) minusOne :: Int -> Int minusOne = (`minus` 1) Functions - guards sgn :: Int -> Int sgn x | x > 0 = 1 | x == 0 = 0 | x < 0 = (-1) f :: Int -> [Char] f x | x /= 1 = "not one" | x /= 2 = "not two" | otherwise = "otherwise" Functions - pattern matching fib :: Integer -> Integer fib 0 = 1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2) Functions - pattern matching head :: [Int] -> Int head (a:as) = a fst :: (Int,Int) -> Int fst (a,b) = a polite :: [Char] -> [Char] polite ('h':'i':s) = 'h':'e':'l':'l':'o':s Functions - function composition f :: Int -> Int f = (^2) g :: Int -> Int g = (+1) k :: Int k = (g . f)(3) Functions - function composition p :: a -> (a, a) p x = (x, x) h :: (a, a) -> Bool -- unknown implementation n :: Bool -> Bool n True = False n False = True x :: a -> Bool x = n . h . p Haskell Basics ● Variables ● Functions ● Control structures ○ if-then-else ○ case of ● More on types Control structures - if-then-else k :: [Char] k = (if 1 + 1 == 2 then "correct" else "incorrect") Control structures - case of k :: [Char] k = (case 1 + 1 of 2 -> "two" 3 -> "not two") arity :: Int -> [Char] arity x = (case x of 0 -> "nullary" 1 -> "unary" 2 -> "binary") Haskell Basics ● Variables ● Functions ● Control structures ● More on types ○ type variables ○ type constraints ○ strong static typing More on types - type variables length' :: [a] -> Int length' (a:as) = 1 + length as length' [] = 0 reverse' :: [a] -> [a] reverse' a = reverse'' a [] where reverse'' (a:as) b = reverse'' as (a:b) reverse'' [] b = b More on types - type constraints (+) :: Num a => a -> a -> a (/) :: Fractional a => a -> a -> a (==) :: Eq a => a -> a -> Bool (:) :: a -> [a] -> [a] (.) :: (b -> c) -> (a -> b) -> (a -> c) ($) :: (a -> b) -> a -> b More on types - strong static typing ● All variables' types are known at compile time ● All function applications have matching types ● Avoids many stupid bugs More on types - strong static typing ● if you can compile, you are probably correct! Haskell standard libraries ● Prelude (imported by default) ● Data.List ● Data.Char https://downloads.haskell.org/~ghc/7.6-latest/docs/html/libraries/index.html Prelude 1 of 5 max :: Ord a => a -> a -> a min :: Ord a
Recommended publications
  • Lambda Calculus and Functional Programming
    Global Journal of Researches in Engineering Vol. 10 Issue 2 (Ver 1.0) June 2010 P a g e | 47 Lambda Calculus and Functional Programming Anahid Bassiri1Mohammad Reza. Malek2 GJRE Classification (FOR) 080299, 010199, 010203, Pouria Amirian3 010109 Abstract-The lambda calculus can be thought of as an idealized, Basis concept of a Turing machine is the present day Von minimalistic programming language. It is capable of expressing Neumann computers. Conceptually these are Turing any algorithm, and it is this fact that makes the model of machines with random access registers. Imperative functional programming an important one. This paper is programming languages such as FORTRAN, Pascal etcetera focused on introducing lambda calculus and its application. As as well as all the assembler languages are based on the way an application dikjestra algorithm is implemented using a Turing machine is instructed by a sequence of statements. lambda calculus. As program shows algorithm is more understandable using lambda calculus in comparison with In addition functional programming languages, like other imperative languages. Miranda, ML etcetera, are based on the lambda calculus. Functional programming is a programming paradigm that I. INTRODUCTION treats computation as the evaluation of mathematical ambda calculus (λ-calculus) is a useful device to make functions and avoids state and mutable data. It emphasizes L the theories realizable. Lambda calculus, introduced by the application of functions, in contrast with the imperative Alonzo Church and Stephen Cole Kleene in the 1930s is a programming style that emphasizes changes in state. formal system designed to investigate function definition, Lambda calculus provides a theoretical framework for function application and recursion in mathematical logic and describing functions and their evaluation.
    [Show full text]
  • Purity in Erlang
    Purity in Erlang Mihalis Pitidis1 and Konstantinos Sagonas1,2 1 School of Electrical and Computer Engineering, National Technical University of Athens, Greece 2 Department of Information Technology, Uppsala University, Sweden [email protected], [email protected] Abstract. Motivated by a concrete goal, namely to extend Erlang with the abil- ity to employ user-defined guards, we developed a parameterized static analysis tool called PURITY, that classifies functions as referentially transparent (i.e., side- effect free with no dependency on the execution environment and never raising an exception), side-effect free with no dependencies but possibly raising excep- tions, or side-effect free but with possible dependencies and possibly raising ex- ceptions. We have applied PURITY on a large corpus of Erlang code bases and report experimental results showing the percentage of functions that the analysis definitely classifies in each category. Moreover, we discuss how our analysis has been incorporated on a development branch of the Erlang/OTP compiler in order to allow extending the language with user-defined guards. 1 Introduction Purity plays an important role in functional programming languages as it is a corner- stone of referential transparency, namely that the same language expression produces the same value when evaluated twice. Referential transparency helps in writing easy to test, robust and comprehensible code, makes equational reasoning possible, and aids program analysis and optimisation. In pure functional languages like Clean or Haskell, any side-effect or dependency on the state is captured by the type system and is reflected in the types of functions. In a language like ERLANG, which has been developed pri- marily with concurrency in mind, pure functions are not the norm and impure functions can freely be used interchangeably with pure ones.
    [Show full text]
  • Deep Dive Into Programming with CAPS HMPP Dev-Deck
    Introduction to HMPP Hybrid Manycore Parallel Programming Московский государственный университет, Лоран Морен, 30 август 2011 Hybrid Software for Hybrid Hardware • The application should stay hardware resilient o New architectures / languages to master o Hybrid solutions evolve: we don‟t want to redo the work each time • Kernel optimizations are the key to get performance o An hybrid application must get the best of its hardware resources 30 август 2011 Москва - Лоран Морен 2 Methodology to Port Applications Methodology to Port Applications Hotspots Parallelization •Optimize CPU code •Performance goal •Exhibit Parallelism •Validation process •Move kernels to GPU •Hotspots selection •Continuous integration Define your Port your Hours to Days parallel application Days to Weeks project on GPU GPGPU operational Phase 1 application with known potential Phase 2 Tuning Optimize your GPGPU •Exploit CPU and GPU application •Optimize kernel execution •Reduce CPU-GPU data transfers Weeks to Months 30 август 2011 Москва - Лоран Морен 4 Take a decision • Go • Dense hotspot • Fast GPU kernels • Low CPU-GPU data transfers • Midterm perspective: Prepare to manycore parallelism • No Go o Flat profile o GPU results not accurate enough • cannot validate the computation o Slow GPU kernels • i.e. no speedup to be expected o Complex data structures o Big memory space requirement 30 август 2011 Москва - Лоран Морен 5 Tools in the Methodology Hotspots Parallelization • Profiling tools Gprof, Kachegrind, Vampir, Acumem, ThreadSpotter •HMPP Workbench •Debugging
    [Show full text]
  • The Quantum IO Monad Thorsten Altenkirch and Alexander S
    1 The Quantum IO Monad Thorsten Altenkirch and Alexander S. Green School of Computer Science, The University of Nottingham Abstract The Quantum IO monad is a purely functional interface to quantum programming implemented as a Haskell library. At the same time it provides a constructive semantics of quantum programming. The QIO monad separates reversible (i.e. unitary) and irreversible (i.e. prob- abilistic) computations and provides a reversible let operation (ulet), allowing us to use ancillas (auxiliary qubits) in a modular fashion. QIO programs can be simulated either by calculating a probability distribu- tion or by embedding it into the IO monad using the random number generator. As an example we present a complete implementation of Shor’s algorithm. 1.1 Introduction We present an interface from a pure functional programming language, Haskell, to quantum programming: the Quantum IO monad, and use it to implement Shor’s factorisation algorithm. The implementation of the QIO monad provides a constructive semantics for quantum programming, i.e. a functional program which can also be understood as a mathematical model of quantum computing. Actually, the Haskell QIO library is only a first approximation of such a semantics, we would like to move to a more expressive language which is also logically sound. Here we are thinking of a language like Agda (Norell (2007)), which is based on Martin L¨of’s Type Theory. We have already investigated this approach of functional specifications of effects in a classical context (Swierstra and Altenkirch (2007, 2008); Swierstra (2008)). At the same 1 2 Thorsten Altenkirch and Alexander S.
    [Show full text]
  • Why Functional Programming Is Good … …When You Like Math - Examples with Haskell
    Why functional programming is good … …when you like math - examples with Haskell . Jochen Schulz Georg-August Universität Göttingen 1/18 Table of contents 1. Introduction 2. Functional programming with Haskell 3. Summary 1/18 Programming paradigm imperative (e.g. C) object-oriented (e.g. C++) functional (e.g. Haskell) (logic) (symbolc) some languages have multiple paradigm 2/18 Side effects/pure functions . side effect . Besides the return value of a function it has one or more of the following modifies state. has observable interaction with outside world. pure function . A pure function always returns the same results on the same input. has no side-effect. .also refered to as referential transparency pure functions resemble mathematical functions. 3/18 Functional programming emphasizes pure functions higher order functions (partial function evaluation, currying) avoids state and mutable data (Haskell uses Monads) recursion is mostly used for loops algebraic type system strict/lazy evaluation (often lazy, as Haskell) describes more what is instead what you have to do 4/18 Table of contents 1. Introduction 2. Functional programming with Haskell 3. Summary 5/18 List comprehensions Some Math: S = fx2jx 2 N; x2 < 20g > [ x^2 | x <- [1..10] , x^2 < 20 ] [1,4,9,16] Ranges (and infinite ranges (don’t do this now) ) > a = [1..5], [1,3..8], ['a'..'z'], [1..] [1,2,3,4,5], [1,3,5,7], "abcdefghijklmnopqrstuvwxyz" usually no direct indexing (needed) > (head a, tail a, take 2 a, a !! 2) (1,[2,3,4,5],[1,2],3) 6/18 Functions: Types and Typeclasses Types removeNonUppercase :: [Char] -> [Char] removeNonUppercase st = [ c | c <- st, c `elem ` ['A'..'Z']] Typeclasses factorial :: (Integral a) => a -> a factorial n = product [1..n] We also can define types and typeclasses and such form spaces.
    [Show full text]
  • Function Programming in Python
    Functional Programming in Python David Mertz Additional Resources 4 Easy Ways to Learn More and Stay Current Programming Newsletter Get programming related news and content delivered weekly to your inbox. oreilly.com/programming/newsletter Free Webcast Series Learn about popular programming topics from experts live, online. webcasts.oreilly.com O’Reilly Radar Read more insight and analysis about emerging technologies. radar.oreilly.com Conferences Immerse yourself in learning at an upcoming O’Reilly conference. conferences.oreilly.com ©2015 O’Reilly Media, Inc. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. #15305 Functional Programming in Python David Mertz Functional Programming in Python by David Mertz Copyright © 2015 O’Reilly Media, Inc. All rights reserved. Attribution-ShareAlike 4.0 International (CC BY-SA 4.0). See: http://creativecommons.org/licenses/by-sa/4.0/ Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Editor: Meghan Blanchette Interior Designer: David Futato Production Editor: Shiny Kalapurakkel Cover Designer: Karen Montgomery Proofreader: Charles Roumeliotis May 2015: First Edition Revision History for the First Edition 2015-05-27: First Release The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Functional Pro‐ gramming in Python, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc.
    [Show full text]
  • What Is Functional Programming? by by Luis Atencio
    What is Functional Programming? By By Luis Atencio Functional programming is a software development style with emphasis on the use functions. It requires you to think a bit differently about how to approach tasks you are facing. In this article, based on the book Functional Programming in JavaScript, I will introduce you to the topic of functional programming. The rapid pace of web platforms, the evolution of browsers, and, most importantly, the demand of your end users has had a profound effect in the way we design web applications today. Users demand web applications feel more like a native desktop or mobile app so that they can interact with rich and responsive widgets, which forces JavaScript developers to think more broadly about the solution domain push for adequate programming paradigms. With the advent of Single Page Architecture (SPA) applications, JavaScript developers began jumping into object-oriented design. Then, we realized embedding business logic into the client side is synonymous with very complex JavaScript applications, which can get messy and unwieldy very quickly if not done with proper design and techniques. As a result, you easily accumulate lots of entropy in your code, which in turn makes your application harder write, test, and maintain. Enter Functional Programming. Functional programming is a software methodology that emphasizes the evaluation of functions to create code that is clean, modular, testable, and succinct. The goal is to abstract operations on the data with functions in order to avoid side effects and reduce mutation of state in your application. However, thinking functionally is radically different from thinking in object-oriented terms.
    [Show full text]
  • Overall Model Final Report
    Project: TERAFLUX - Exploiting dataflow parallelism in Teradevice Computing Grant Agreement Number: 249013 Call: FET proactive 1: Concurrent Tera-device Computing (ICT-2009.8.1) SEVENTH FRAMEWORK PROGRAMME THEME FET proactive 1: Concurrent Tera-Device Computing (ICT-2009.8.1) PROJECT NUMBER: 249013 Exploiting dataflow parallelism in Teradevice Computing D3.5 – Overall Computational Model Final Report Due date of deliverable: 31 March 2014 Actual Submission: 19 th May 2014 Start date of the project: January 1st , 2010 Duration: 51 months Lead contractor for the deliverable: UNIMAN Revision : See file name in document footer. Project co-founded by the European Commission within the SEVENTH FRAMEWORK PROGRAMME (2007-2013) Dissemination Level: PU PU Public PP Restricted to other programs participant (including the Commission Services) RE Restricted to a group specified by the consortium (including the Commission Services) CO Confidential, only for members of the consortium (including the Commission Services) Change Control Version# Author Organization Change History 1 Mikel Lujan UNIMAN 2 Mikel Lujan UNIMAN Improvements based on internal feedback 3 Mikel Lujan UNIMAN Improvements based on internal review 4 Roberto Giorgi UNISI Coordinator’s review Release Approval Name Role Date Mikel Lujan Originator 31/Mar/2014 Mikel Lujan WP leader 30/Apr/2014 Roberto, Giorgi Project Coordinator for formal deliverable 11/May/1014 Deliverable number: D3.5 Deliverable name: Overall Computational Model Final Report File name: TERAFLUX-D35-v4.doc Page 1 of 48 Project: TERAFLUX - Exploiting dataflow parallelism in Teradevice Computing Grant Agreement Number: 249013 Call: FET proactive 1: Concurrent Tera-device Computing (ICT-2009.8.1) TABLE OF CONTENT TABLE OF CONTENT .................................................................................................................................
    [Show full text]
  • A Brief Overview of Functional Programming Languages
    electronic Journal of Computer Science and Information Technology (eJCSIT), Vol. 6, No. 1, 2016 A Brief Overview of Functional Programming Languages Jagatheesan Kunasaikaran1, Azlan Iqbal2 1ZALORA Malaysia, Jalan Dua, Chan Sow Lin, Kuala Lumpur, Malaysia e-mail: [email protected] 2College of Computer Science and Information Technology, Universiti Tenaga Nasional, Putrajaya Campus, Selangor, Malaysia e-mail: [email protected] Abstract – Functional programming is an important Common programming paradigms are procedural, object- programming paradigm. It is based on a branch of oriented and functional. Figure 1 illustrates conceptually mathematics known as lambda calculus. In this article, we the differences between the common programming provide a brief overview, aimed at those new to the field, and language paradigms [1]. Procedural and object-oriented explain the progress of functional programming since its paradigms mutate or alter the data along program inception. A selection of functional languages are provided as examples. We also suggest some improvements and speculate execution. On the other hand, in pure functional style, the on the potential future directions of this paradigm. data does not exist by itself or independently. A composition of function calls with a set of arguments Keywords – functional, programming languages, LISP, Python; generates the final result that is expected. Each function is Javascript,Java, Elm, Haskell ‘atomic’ as it only executes operations defined in it to the input data and returns the result of the computation to the ‘callee’. I. INTRODUCTION II. FUNCTIONAL LANGUAGES Programming languages can be classified into the style of programming each language supports. There are multiple Functional programming is based on mathematical logic.
    [Show full text]
  • D: a Programming Language for Our Time
    D: A Programming Language for Our Time Charles D. Allison Utah Valley University Orem, UT 84058 801-863-6389 [email protected] Summer 2010 Abstract Modern programming languages such as C#, Ruby and Java have much in common: they support (and some require) object-oriented programming, have large, useful libraries, manage memory via garbage collection, and to a large degree shield the programmer from low-level details. They also tend to be interpreted languages, so while they are applicable to most programming problems, they lack the efficiency of systems programming languages like C and C++. The D Programming Language is a modern hybrid of C++ and languages like Ruby: it compiles statically to native code, but is also garbage collected. It inherits the C and C++ libraries, but has its own modern library. Like C++, it is a multi-paradigm language, supporting imperative, object-oriented and functional programming styles. Its pure functional subset is suitable for teaching the functional paradigm in a survey of languages course. It also supports delegates and anonymous functions, and has a number of software engineering features built-in. This paper explores how D is suitable for courses at various levels in the CS curriculum as well as in the workplace. Introduction Computer Science curricula seek to ground students in the fundamentals of computation while also preparing them for graduate study as well as to make meaningful contributions to current computing problems in the workplace. But traditional CS curricula have recently proven unattractive to many of today’s students resulting in falling CS enrollments, while the need for competent software professionals is increasing.[1] Feedback from industry indicates that areas such as software security, parallelism, quality, performance, and software engineering best practices need more emphasis in CS-related degree programs, while not neglecting the foundations of computing.
    [Show full text]
  • LAMBDA CALCULUS for ENGINEERS Introduction in the Λ
    LAMBDA CALCULUS FOR ENGINEERS PIETER H. HARTEL AND WILLEM G. VREE Faculty of Electrical Engineering, Mathematics and Computer Science, University of Twente e-mail address: [email protected] Faculty of Technology, Policy and Management, Technical University of Delft e-mail address: [email protected] Abstract. In pure functional programming it is awkward to use a stateful sub-computation in a predominantly stateless computation. The problem is that the state of the sub- computation has to be passed around using ugly plumbing. Classical examples of the plumbing problem are: providing a supply of fresh names, and providing a supply of random numbers. We propose to use (deterministic) inductive definitions rather than recursion equations as a basic paradigm and show how this makes it easier to add the plumbing. Introduction In the λ-calculus, the variable convention [2, 2.1.13] states that “If M1,... Mn occur in a certain mathematical context (e.g. definition, proof), then in these terms all bound variables are chosen to be different from the free variables”. One such mathematical context is the defining equation of substitution for a lambda abstraction: (λy.M)[x := N] = λy.M[x := N] To adhere to the variable convention, it may be necessary to rename bound variables, which in turn requires a supply of fresh names. In the substitution example below z is a name that appears only here: (λy.x y)[x := y] ≡ (λz.y z) The authors are engineers, and like other engineers, we are concerned with this seem- ingly trivial issue of providing freshness: over the past 20 years a number of papers have been wholly or partly devoted to solutions of providing fresh names, including Peyton Jones [11, Chapter 9], Augustsson, Rittri and Synek [1], Launchbury [10], Boulton [3], Peyton Jones and Marlow [12], Shinwell, Pitts and Gabbay [13], Cheney and Urban [5], and Cheney [4].
    [Show full text]
  • Functional Programming Languages Part IV: Monadic Transformations, Monadic Programming
    Functional programming languages Part IV: monadic transformations, monadic programming Xavier Leroy INRIA Paris-Rocquencourt MPRI 2-4, 2016–2017 X. Leroy (INRIA) Functional programming languages MPRI 2-4, 2016–2017 1 / 81 Monads in programming language theory Monads are a technical device (inspired from category theory) with several uses in programming: To structure denotational semantics and make them easy to extend with new language features. (E. Moggi, 1989.) Not treated in this lecture. To factor out commonalities between many program transformations and between their proofs of correctness. As a powerful programming techniques in pure functional languages. (P. Wadler, 1992; the Haskell community). X. Leroy (INRIA) Functional programming languages MPRI 2-4, 2016–2017 2 / 81 Outline 1 Introduction to monads 2 The monadic translation Definition Correctness Application to some monads 3 Monadic programming More examples of monads Monad transformers 4 Bonus track: applicative structures 5 Bonus track: comonads X. Leroy (INRIA) Functional programming languages MPRI 2-4, 2016–2017 3 / 81 Introduction to monads Commonalities between program transformations Consider the conversions to exception-returning style, state-passing style, and continuation-passing style. For constants, variables and λ-abstractions, we have: [[N]] = V (N) [[N]] = λs.(N, s) [[N]] = λk.k N [[x]] = V (x) [[x]] = λs.(x, s) [[x]] = λk.k x [[λx.a]] = V (λx.[[a]]) [[λx.a]] = λs.(λx.[[a]], s) [[λx.a]] = λk.k (λx.[[a]]) in all three cases, we return (put in some appropriate wrapper) the values N or x or λx.[[a]]. X. Leroy (INRIA) Functional programming languages MPRI 2-4, 2016–2017 4 / 81 Introduction to monads Commonalities between program transformations For let bindings, we have: [[let x = a in b]] = match [[a]] with E(x) E(x) V (x) [[b]] → | → [[let x = a in b]] = λs.
    [Show full text]