Haskell Answers 5: map and filter

Similar documents
Programming Fundamentals. Lesson 20 Sections

Cardinality. The set of all finite strings over the alphabet of lowercase letters is countable. The set of real numbers R is an uncountable set.

Conditionals (with solutions)

Solutions for Practice problems on proofs

3.1. RATIONAL EXPRESSIONS

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

Special Directions for this Test

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

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

Portal Connector Fields and Widgets Technical Documentation

General Certificate of Education Advanced Subsidiary Examination June 2015

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Multiplication. Year 1 multiply with concrete objects, arrays and pictorial representations

Derivative Approximation by Finite Differences

Python Lists and Loops

Get post program 7 W.a.p. that Enter two numbers from user and do arithmetic operation

Software Development Phases

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

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

INTRODUCTION TO FUNCTIONAL PROGRAMMING Take-Home Exam

arrays C Programming Language - Arrays

Basic Proof Techniques

Pigeonhole Principle Solutions

Category 3 Number Theory Meet #1, October, 2000

Sources: On the Web: Slides will be available on:

Number Representation

Factoring Polynomials

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

Useful Microsoft Excel Functions & Formulas Theresa A Scott, MS Department of Biostatistics Vanderbilt University theresa.scott@vanderbilt.

MACM 101 Discrete Mathematics I

6.3 FACTORING ax 2 bx c WITH a 1

The if Statement and Practice Problems

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

South East of Process Main Building / 1F. North East of Process Main Building / 1F. At 14:05 April 16, Sample not collected

Cubes and Cube Roots

Prime Time: Homework Examples from ACE

Sample Questions Csci 1112 A. Bellaachia

Moving from CS 61A Scheme to CS 61B Java

4/1/2017. PS. Sequences and Series FROM 9.2 AND 9.3 IN THE BOOK AS WELL AS FROM OTHER SOURCES. TODAY IS NATIONAL MANATEE APPRECIATION DAY

J a v a Quiz (Unit 3, Test 0 Practice)

Factoring and Applications

Factoring Polynomials

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013


Project 2: Bejeweled

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

CS 241 Data Organization Coding Standards

CmpSci 187: Programming with Data Structures Spring 2015

1.2. Successive Differences

Functional Programming in C++11

Ralf Hinze Generic Programs and Proofs

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

k, then n = p2α 1 1 pα k

Regular Expressions and Automata using Haskell

Combinatorial Proofs

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

of Nebraska - Lincoln

Lecture 2: Regular Languages [Fa 14]

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

Example of a Java program

s = s = s =

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

FACTORING ax 2 bx c. Factoring Trinomials with Leading Coefficient 1

SECTION 10-2 Mathematical Induction

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Creating Basic Excel Formulas

Lab 7 Keyboard Event Handling Mouse Event Handling

SIMPLIFYING SQUARE ROOTS EXAMPLES

6.4 Special Factoring Rules

1.3 Polynomials and Factoring

Sequence of Numbers. Mun Chou, Fong QED Education Scientific Malaysia

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Detecting Pattern-Match Failures in Haskell. Neil Mitchell and Colin Runciman York University

Mathematical Induction

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

SOLVING QUADRATIC EQUATIONS - COMPARE THE FACTORING ac METHOD AND THE NEW DIAGONAL SUM METHOD By Nghi H. Nguyen

The design recipe. Programs as communication. Some goals for software design. Readings: HtDP, sections 1-5

Midterm Practice Problems

15 Prime and Composite Numbers

VB.NET - STRINGS. By calling a formatting method to convert a value or object to its string representation

Lecture 11: Tail Recursion; Continuations

Some Scanner Class Methods

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

Data Management Applications with Drupal as Your Framework

SECTION 2: Working with Functions and Formulas

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Recursion and Recursive Backtracking

Working with whole numbers

6.2 Permutations continued

Just the Factors, Ma am

SOME EXCEL FORMULAS AND FUNCTIONS

MS-EXCEL: ANALYSIS OF EXPERIMENTAL DATA

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

SOLUTIONS FOR PROBLEM SET 2

Predicate Logic. Example: All men are mortal. Socrates is a man. Socrates is mortal.

XSLT Mapping in SAP PI 7.1

Transcription:

Haskell Answers 5: map and filter Antoni Diller 4 August 2011 (1) The type String is the same as [Char]. Define a function capitalises, of type String String, which takes a list of characters as its argument and returns the same list as its value except that each lower-case letter has been replaced by its upper-case equivalent. Thus, capitalises "Minority Report" = "MINORITY REPORT". capitalises :: String -> String capitalises = map toupper (2) Define a function squareall :: [Int] [Int] which takes a list of integers and produces a list of the squares of those integers. For example, squareall [6, 1, (-3)] = [36, 1, 9]. squareall :: [Int] -> [Int] squareall = map (\x -> x*x) (3) Define a function nestedreverse which takes a list of strings as its argument and reverses each element of the list and then reverses the resulting list. Thus, nestedreverse ["in", "the", "end"] = ["dne", "eht", "ni"]. nestedreverse :: [String] -> [String] nestedreverse = reverse. map reverse (4) Define a function atfront :: a [[a]] [[a]] which takes an object and a list of lists and sticks the object at the front of every component list. For example, atfront 7 [[1,2], [], [3]] = [[7,1,2], [7], [7,3]]. atfront :: a -> [[a]] -> [[a]] atfront x xss = map (x:) xss 1

(5) Define a function lengths which takes a list of strings as its argument and returns the list of their lengths. For example, lengths ["the", "end", "is", "nigh"] = [3, 3, 2, 4]. lengths :: [String] -> [Int] lengths = map length (6) Define a function parity :: [String] [Int] which takes a list of strings and returns a list of the integers 0 and 1 such that 0 is the nth element of the value if the nth string of the argument contains an even number of characters and 1 is the nth element of the value if the nth string contains an odd number of characters. For example, parity ["one", "two", "three", "four"] = [1, 1, 1, 0]. parity :: [String] -> [Int] parity xss = map f xss where f xs even (length xs) = 0 otherwise = 1 (7) Using the higher-order function map define a function sumsq which takes an integer n as its argument and returns the sum of the squares of the first n integers. That is to say, square :: Num a => a -> a square x = x * x sumsq n = 1 2 + 2 2 + 3 2 +... + n 2. sumsq :: Integral a => a -> a sumsq n = sum (map square [1..n]) (8) Define a function subseqs which takes a finite list xs as its argument and returns the list of all the subsequences of xs. (A subsequence of xs is a selection of not necessarily adjacent elements of xs which appear in their original order.) subseqs :: [a] -> [[a]] subseqs [] = [[]] subseqs (x:xs) = (subseqs xs) ++ map (x:) (subseqs xs) (9) The function filter can be defined in terms of concat and map: 2

filter p = concat.map box where box x =... Complete this definition of filter by defining box. filter1 :: (a -> Bool) -> [a] -> [a] filter1 p = concat.map box where box x p x = [x] otherwise = [] (10) Define a function wc (without capitals) which removes all the capital letters from a string. Thus, wc "Mark Twain" = "ark wain". wc :: String -> String wc = filter (not.isupper) (11) Define a function wp (without primes) which removes all the primes from a list of numbers. Thus, wp [1, 2, 3, 4, 5, 6, 7] = [1, 4, 6]. wp :: [Int] -> [Int] wp = filter (not.prime) auxprime :: Integral a => a -> a -> Bool auxprime i 2 = i rem 2 == 0 auxprime i j = i rem j == 0 auxprime i (j-1) prime :: Integral a => a -> Bool prime 1 = False prime 2 = True prime i = not (auxprime i (i-1)) (12) Define a function wtel (without the empty list) which removes every occurrence of the empty list from a list of lists. Thus, wtel [[1, 2], [], [1, 3]] = [[1, 2], [1, 3]]. wtel :: [[a]] -> [[a]] wtel = filter (not.null) (13) Define a function caen (containing an even number) which takes a list of lists of integers as its argument and removes from it every list not containing an even 3

number. Thus, caen [[1,3], [2,1], [7,9], [2, 4, 8]] = [[2,1], [2, 4, 8]]. caen :: [[Int]] -> [[Int]] caen = filter (even.product) (14) Define a function afoae (at front of all even) which takes an integer and a list of lists of integers as its two arguments. It removes every element from the list which contains at least one odd number and attaches the integer at the front of the remaining lists. For example, afoae 7 [[2, 4], [2, 3], [3, 7], [3, 4], [6, 100]] = [[7, 2, 4], [7, 6, 100]]. afoae :: Int -> [[Int]] -> [[Int]] afoae i jss = map (i:) (filter (and.map even) (filter (not.null) jss)) (15) Define a function wvowel (without vowels) which removes every occurrence of a vowel from a list of characters. wvowel :: String -> String wvowel xs = filter f xs where f x = not (x == a x == e x == i x == o x == u ) (16) Define a function wiv (without internal vowels) which takes a list of strings as its argument and removes every occurrence of a vowel from each element. For example, wiv ["the", "end", "is", "nigh"] = ["th", "nd", "s", "ngh"]. wiv :: [String] -> [String] wiv = map wvowel (17) Define a function ssp (sum the squares of primes) which takes a list of integers as its argument, removes those that are not primes and then squares the remaining integers and then adds the results together. For example, ssp [2, 4, 7, 1, 3] = 62. 4

ssp :: [Int] -> Int ssp = sum. map square. filter prime 5