Haskell Modules Roshan Gunathilake.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

15-Jan-15 More Haskell Functions Maybe, Either, List, Set, Map.
Haskell Chapter 6. Modules  A module defines some functions, types, and type classes  A program is a collection of modules  Module used in GHCi is.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
The Binary Numbering Systems
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Agenda Data Representation – Characters Encoding Schemes ASCII
IT253: Computer Organization
Computer Structure & Architecture 7c - Data Representation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Modules.
WHAT IS A DATABASE? A DATABASE IS A COLLECTION OF DATA RELATED TO A PARTICULAR TOPIC OR PURPOSE OR TO PUT IT SIMPLY A GENERAL PURPOSE CONTAINER FOR STORING.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
16-Nov-15 More Haskell Functions Maybe, Either, List, Set, Map.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
0 Modules in Haskell Adapted from material by Miran Lipovaca.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
1 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Type declarations.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Haskell Chapter 6.
Sets and Maps Chapter 9.
EET 2259 Unit 13 Strings and File I/O
SQL – Python and Databases
VBA - Excel VBA is Visual Basic for Applications
C Characters and Strings
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Haskell Chapter 2.
More Haskell Functions
ECE Application Programming
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
Basic Data Structures.
Multiple variables can be created in one declaration
PROGRAMMING IN HASKELL
Haskell.
More Haskell Functions
PROGRAMMING IN HASKELL
Representing Characters
Basic Data Structures.
PROGRAMMING IN HASKELL
Simplifying Flow of Control
CHAPTER THREE Sequences.
String Manipulation Chapter 7 Attaway MATLAB 4E.
More Haskell Functions
Python Primer 1: Types and Operators
Fundamentals of Functional Programming
More Haskell Functions
Sets and Maps Chapter 9.
CS 1111 Introduction to Programming Spring 2019
CSCE 314: Programming Languages Dr. Dylan Shell
Visual Programming COMP-315
EET 2259 Unit 13 Strings and File I/O
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Lexical Elements & Operators
PROGRAMMING IN HASKELL
ASCII and Unicode.
Presentation transcript:

Haskell Modules Roshan Gunathilake

Introduction A Haskell module is a collection of related functions, types and type classes. A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them to do something. Modules improve the reusability and makes the code more manageable.

Introduction The haskell Standard Library contains of a set of Modules. Eg : Lists, Concurrency, Complex Numbers One script can import several modules. Syntax import <module name>

Import Modules Ex 1: import Data.Char putStrLn("Different methods of Char Module") print(toUpper 'a') print(words "Let us study tonight") print(toLower 'A') We can selectively import specific functions Ex 2 : import Data.Char(toUpper)

Import Modules You can also choose to import all of the functions of a module except few selected ones Ex 3: import Data.Char hiding(toUpper) Qualified imports can be used to avoid confusion of names from functions in different modules. Ex 4: import qualified Data.Map as M Now, to reference Data.Map’s filter function, we just use M.filter.

Hoogle Haskell search engine, you can search by name, module name or even type signature.

Data.List Data.List module is all about lists. It provides some useful functions about lists. Intersperse : takes an element of a list and then puts that element in between each pair of elements in the list. Ex5 & 6: intersperse '.' "MONKEY" "M.O.N.K.E.Y" intersperse 0 [1,2,3,4,5,6] [1,0,2,0,3,0,4,0,5,0,6]

Data.List Intercalate : takes a list of lists and a list. It then inserts that list in between all those lists and then flattens the result. Ex 7 & 8: intercalate " " ["hey","there","guys"] "hey there guys" intercalate [0,0,0] [[1,2,3],[4,5,6],[7,8,9]] [1,2,3,0,0,0,4,5,6,0,0,0,7,8,9]

Data.List transpose : transposes a list of lists. If you look at a list of lists as a 2D matrix, the columns become the rows and vice versa. Ex 9 & 10: transpose [[1,2,3],[4,5,6],[7,8,9]] [[1,4,7],[2,5,8],[3,6,9]] transpose ["hey","there","guys"] ["htg","ehu","yey","rs","e"]

Data.List concat : flattens a list of lists into just a list of elements. Ex 11 & 12: concat ["foo","bar","car"] "foobarcar" concat [[3,4,5],[2,3,4],[2,1,1]] [3,4,5,2,3,4,2,1,1]

Data.List and , or operators Ex 13 & 14: and $ map (>4) [5,6,7,2] or $ map (>4) [5,6,7,2] iterate : takes a function and a starting value. It applies the function to the starting value, then it applies that function to the result, then it applies the function to that result again, etc. It returns all the results in the form of an infinite list. Ex 15: take 10 $ iterate (*2) 1 [1,2,4,8,16,32,64,128,256,512]

Data.List splitAt : takes a number and a list. It then splits the list at that many elements, returning the resulting two lists in a tuple. Ex 16: splitAt 3 "heyman" ("hey","man") sort : simply sorts a list. The type of the elements in the list has to be part of the Ord typeclass, because if the elements of a list can't be put in some kind of order, then the list can't be sorted. Ex 17: sort [8,5,3,2,1,6,4,2] [1,2,2,3,4,5,6,8]

Data.List partition : takes a list and a predicate and returns a pair of lists. The first list in the result contains all the elements that satisfy the predicate, the second contains all the ones that don't. Ex 18: partition (>3) [1,3,5,6,3,2,1,0,3,7] ([5,6,7],[1,3,3,2,1,0,3]) union : also acts like a function on sets. It returns the union of two lists. Ex 19: "hey man" `union` "man what's up" "hey manwhat'sup"

Data.List insert : takes an element and a list of elements that can be sorted and inserts it into the last position where it's still less than or equal to the next element. In other words, insert will start at the beginning of the list and then keep going until it finds an element that's equal to or greater than the element that we're inserting and it will insert it just before the element. Ex 20: insert 4 [3,5,1,2,8,2] [3,4,5,1,2,8,2]

Data.Char Data.Char module does what its name suggests. It exports functions that deal with characters. It's also helpful when filtering and mapping over strings because they're just lists of characters. isControl checks whether a character is a control character. isSpace checks whether a character is a white-space characters. That includes spaces, tab characters, newlines, etc. isLower checks whether a character is lower-cased.

Data.Char isUpper checks whether a character is upper-cased. isAlpha checks whether a character is a letter. isAlphaNum checks whether a character is a letter or a number. isPrint checks whether a character is printable. Control characters, for instance, are not printable. isDigit checks whether a character is a digit. isOctDigit checks whether a character is an octal digit. isHexDigit checks whether a character is a hex digit.

Data.Char isLetter checks whether a character is a letter. isMark checks for Unicode mark characters. Those are characters that combine with preceding letters to form latters with accents. Use this if you are French. isNumber checks whether a character is numeric. isPunctuation checks whether a character is punctuation. isSymbol checks whether a character is a fancy mathematical or currency symbol. isSeparator checks for Unicode spaces and separators.

Data.Char isAscii checks whether a character falls into the first 128 characters of the Unicode character set. isLatin1 checks whether a character falls into the first 256 characters of Unicode. isAsciiUpper checks whether a character is ASCII and upper-case. isAsciiLower checks whether a character is ASCII and lower-case.

Data.Map Association lists (also called dictionaries) are lists that are used to store key-value pairs where ordering doesn't matter. For instance, we might use an association list to store phone numbers, where phone numbers would be the values and people's names would be the keys. Ex 21: import qualified Data.Map as Map Map.empty formlist[] Map.insert 5 600 ( Map. nsert 4 200 ( Map.insert 3 100 Map.empty )) fromList [(3 ,100) ,(4 ,200) ,(5 ,600)]

Data.Set Sets are kind of like a cross between lists and maps. All the elements in a set are unique. And because they're internally implemented with trees (much like maps in Data.Map), they're ordered. Checking for membership, inserting, deleting, etc. is much faster than doing the same thing with lists. Ex 22: import qualified Data.Set as Set0 set1 = Set0.fromList "Roshan" set2 = Set0.fromList "Yang“ Set0.intersection set1 set2 fromList "an“ Set0.union set1 set2 fromList "RYaghnos"

Making our own Modules We can create our own custom modules in addition to Haskell's existing modules. At the beginning of a module, we specify the module name. File name and Module Name must be the same. Ex 22: Loading custom modules

Making our own Modules Modules can also be given a hierarchical structures. Each module can have a number of sub-modules and they can have sub-modules of their own. First, we’ll make a folder called Geometry. Mind the capital G. In it, we’ll place 2 files:

Making our own Modules Sphere.hs module Geometry.Cuboid ( volume , area ) where volume :: Float -> Float -> Float -> Float volume a b c = rectangleArea a b * c area :: Float -> Float -> Float -> Float area a b c = rectangleArea a b * 2 + rectangleArea a c * 2 + rectangleArea c b * 2 rectangleArea :: Float -> Float -> Float rectangleArea a b = a * b

Making our own Modules Cube.hs module Geometry.Cube ( volume , area ) where import qualified Geometry.Cuboid as Cuboid volume :: Float -> Float volume side = Cuboid.volume side side side area :: Float -> Float area side = Cuboid.area side side side

Making our own Modules How to import Now : import Geometry.Sphere

Summary Introduction Syntaxes Importing Haskell Modules Haskell Search Engine Data.List, Data.Char, Data.Map, Data.Set Creating our own Haskell Modules