Programming Fundamentals. Lesson 20 Sections



Similar documents
The countdown problem


Introduction to Python

SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University

Chapter 2: Elements of Java

CSc 372. Comparative Programming Languages. 21 : Haskell Accumulative Recursion. Department of Computer Science University of Arizona

Functional Programming

MAT Program Verication: Exercises

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Monads for functional programming

Illustration 1: Diagram of program function and data flow

9/23/

Habanero Extreme Scale Software Research Project

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Lecture 12: Abstract data types

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Special Directions for this Test

Chapter 15 Functional Programming Languages

Creating Database Tables in Microsoft SQL Server

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Practice Questions. CS161 Computer Security, Fall 2008

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

CS 1133, LAB 2: FUNCTIONS AND TESTING

Programming Languages in Artificial Intelligence

Type Classes with Functional Dependencies

GJC Web Design Virtuemart 2.0 Radius Shipping Plugin

Functional Programming in C++11

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

p-values and significance levels (false positive or false alarm rates)

FUNCTIONAL PROGRAMMING

Datatype-generic data migrations

Documentum Developer Program

Java Crash Course Part I

Using SQL Server Management Studio

A) the use of different pens for writing B) learning to write with a pen C) the techniques of writing with the hand using a writing instrument

Computational Semantics, Type Theory, and Functional Programming

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

[MD5 Message Digests] derived from the RSA Data Security, Inc. MD5 Message Digest Algorithm

High level code and machine code

Dependent Types at Work

KU BUSINESS CARD OPTIONS Standard Horizontal: 1-sided (Blank Back)

Discrete Maths. Philippa Gardner. These lecture notes are based on previous notes by Iain Phillips.

Qt Signals and Slots. Olivier Goffart. October 2013

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Java Interview Questions and Answers

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

MODELLING. IF...THEN Function EXCEL Wherever you see this symbol, make sure you remember to save your work!

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Google s MapReduce Programming Model Revisited

Fun with Phantom Types

Understanding how people read your direct mail results in higher response rates

SEF s Mission. SEF Portuguese Immigration and Borders Service

Describing, manipulating and pricing financial contracts: The MLFi language

Data Management Applications with Drupal as Your Framework

Integer Set Library: Manual

Variables, Constants, and Data Types

Microsoft Access 3: Understanding and Creating Queries

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

String Search. Brute force Rabin-Karp Knuth-Morris-Pratt Right-Left scan

A Grammar for the C- Programming Language (Version S16) March 12, 2016

Get Success in Passing Your Certification Exam at first attempt!

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Developing Web Applications for Microsoft SQL Server Databases - What you need to know

Lecture Data Types and Types of a Language

U-HID Pac-LED64 PacDrive U-HID LEDs

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Braindumps.C questions

The Advantages and Disadvantages of Network Computing Nodes

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Introduction FUNCTIONAL PROGRAMMING. The Problem. The Solution. Graham Hutton. Lecture 9 - Interactive Programs. keyboard. screen

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

SQL Server An Overview

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

How To Program In Scheme (Prolog)

3.GETTING STARTED WITH ORACLE8i

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Selection Statements

Practical Generic Programming with OCaml

Objective-C Tutorial

TypeScript for C# developers. Making JavaScript manageable

Real-time Streaming Analysis for Hadoop and Flume. Aaron Kimball odiago, inc. OSCON Data 2011

Member Functions of the istream Class

7. Show that the expectation value function that appears in Lecture 1, namely

Transcription:

Programming Fundamentals Lesson 20 Sections

Today, we will Study sections, in Haskell. Sections are another class of expressions that represent functions. A section is a binary operation where one of the operands is missing. It represents the unary function in which the missing operand is the argument, so to speak For example, the section (>=10) represents the function (\x -> x >= 10) 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 2

Section examples Let s try some sections at the console: Main> (++ " do algarve") "universidade" "universidade do algarve" Main> (++ " do algarve") "orquestra" "orquestra do algarve" Main> ("grande " ++) "carro" "grande carro" Main> ("grande " ++) "festa" "grande festa" Main> (`mod` 100) 1234 34 Main> (100 `mod`) 32 4 Main> (100 `mod`) 43 14 Main> (`min` 20) 20.3 20.0 Main> (`min` 20) 18.3 18.3 Main> (/2) 2891 1445.5 Main> (2/) 2891 0.000691802144586648 Main> (+1) 777 778 Main> (<= 20) 20.3 False Main> (*10) 64 640 Main> (10^) 6 1000000 Main> (^10) 6 60466176 Main> (1/) 50 0.02 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 3

Section examples Sections, as lambda expressions, are typically used as arguments to higher order functions: Hugs> map (+1) [5,8,2] [6,9,3] Hugs> map (*2) [5,8,2] [10,16,4] Hugs> map (`min` 20) [19.1, 20.4, 21.4, 16.9, 17.7] [19.1,20.0,20.0,16.9,17.7] Hugs> map (++ ", algarve") ["faro", "tavira", "silves"] ["faro, algarve","tavira, algarve","silves, algarve"] Hugs> filter (<=10) [5,8,12,5,23,44,7,9] [5,8,5,7,9] Hugs> filter (/= 0) [0,3,0,1,1,2] [3,1,1,2] Hugs> map (2^) [0..10] [1,2,4,8,16,32,64,128,256,512,1024] Hugs> map (1/) (map (2^) [0..10]) [1.0,0.5,0.25,0.125,0.0625,0.03125,0.015625,0.0078125,0.0 0390625,0.001953125,0.0009765625] Hugs> until (<0.001) (/2) 1 0.0009765625 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 4

Quicksort with sections Using sections, we can further simplify quicksort: qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (x : xs) = qsort (filter (<= x) xs) ++ [x] ++ qsort (filter (> x) xs) This is the standard version of quicksort in Haskell. Remember it, when you have to program quicksort in other languages, in the future. 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 5

More exemplary functions The most significant digit: front :: Int -> Int front = until (<10) (`div` 10) The last element: lastelem :: [a] -> a This function is defined by a function expression. Main> front 374655 3 Main> lastelem [1..512] 512 lastelem = head. until (null. tail) tail 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 6

Functions with multiple arguments Indeed, a function signature such as f :: a -> b -> c must be read as f :: a -> (b -> c). Thus, f :: a -> b -> c really means that f is a function with an argument of type a and a result of type (b -> c). But (b -> c) is the type of functions with an argument of type b and result of type c. This implies that, in this case, an expression f x, where x is an expression of type a, represents a function from b to c. We conclude that f is a function with one argument and a function result, and not a function with two arguments. 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 7

Examples of function calls with missing arguments Note that the missing argument must be the last. Function calls with missing arguments are expressions that represent functions, and can be used as arguments to higher order functions, just like sections: Hugs> filter (elem 'a') ["faro", "aveiro", "porto"] ["faro","aveiro"] Main> map (take 2) ["faro", "aveiro", "porto"] ["fa","av","po"] Main> map (div 100) (map (^2) [1..10]) [100,25,11,6,4,2,2,1,1,1] map (max 10) [6,9,12,3,45,8] [10,10,12,10,45,10] Main> map (zip [1..1000]) ["lisboa", "porto"] [[(1,'l'),(2,'i'),(3,'s'),(4,'b'),(5,'o'),(6,'a')],[ (1,'p'),(2,'o'),(3,'r'),(4,'t'),(5,'o')]] 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 8

IsVowel A function to check whether a char is a owercase vowel: isvowel :: Char -> Bool -- isvowel x = elem x "aeiou" -- isvowel x = flip elem "aeiou" x -- isvowel x = ((flip elem) "aeiou") x isvowel = flip elem "aeiou" Main> isvowel 's' False Main> isvowel 'o' True Main> filter isvowel "portalegre" "oaee" Main> filter (not. isvowel) "portalegre" "prtlgr" Main> filter (not. flip elem "aeiou") "portalegre" 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 9 "prtlgr" flip' :: (a -> b -> c) -> (b -> a -> c) flip' f x y = f y x

Exercises Program a function to remove all vowels from a string. Consider the following two functions: f :: Integral a => (a, Int) -> (a, Int) f (x, y) = (div x 10, y+1) d :: Integral a => a -> Int d x = snd (until ((==0).fst) f (x, 0)) What does function d compute? Program a function compute the length of a list, based on the idea embodied by functions d and f above. 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 10

Control How many arguments does a Haskell function have? What is a section? What does flip (/) mean? What does a function call with a missing last argument represent? 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 11

Tomorrow We will study folds, a group of important higher order functions. 23-Jan-11 Programming Fundamentals - 20 Pedro Guerreiro 12