Text Files and Random Numbers

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Fall 2008Programming Development Techniques 1 Topic 9 Symbol Manipulation Generating English Sentences Section This is an additional example to symbolic.
Getting Started with Haskell Tim Sheard. Learning a new language Goals – Learn how to design and use data structures – Learn to write programs using the.
Computer Science 111 Fundamentals of Programming I Sequences: Lists.
Fundamentals of Python: From First Programs Through Data Structures
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Fundamentals of Python: First Programs
Computer Science 112 Fundamentals of Programming II Recursive Processing of Languages.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types.
Computer Science 111 Fundamentals of Programming I Dictionaries redux.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
The man bites the dog man bites the dog bites the dog the dog dog Parse Tree NP A N the man bites the dog V N NP S VP A 1. Sentence  noun-phrase verb-phrase.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
Haskell Chapter 9. More Input and More Output  Files and Streams  Transforming Input  Not covered  brackets  command-line arguments  bytestrings.
Data in Haskell. What is data? Measurements of some kind stored in a computer. – Examples Simple: numbers, text, truth values … Structured: sequences,
1 Simple Lisp Programs References:
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
Två saker ● Labsal bokning ● Sprid ut er över salarna ● “Bracket Matching” [ " Slides: " ++ concat (intersperse ", " (map lnk gs)) | let gs = [ f | f.
Sorts, CompareTo Method and Strings
Grammars and Parsing.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Introduction to Python
Fundamentals of Programming II Recursive Processing of Languages
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Fundamentals of Programming I A Sentence Generator
Data Types and Structures
Chapter 7 Text Input/Output Objectives
Haskell Chapter 9.
Fundamentals of Programming I Dictionaries redux
Fundamentals of Programming I Design with Functions
Haskell programming language.
Fundamentals of Programming I Managing the Namespace
Programming In Any Language With “Hello, World!” Examples
Introduction to Python
CSC115 Introduction to Computer Programming
Syntax.
Introduction to Python
Haskell Strings and Tuples
Computer Science 312 Haskell Lists 1.
Natural Language Processing
Haskell Dealing with impurity 30-Nov-18.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
ARRAYS 1 GCSE COMPUTER SCIENCE.
String What it is Why it’s useful
Coding Concepts (Data- Types)
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Python Basics with Jupyter Notebook
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Introduction to Computer Science
More on Creating Classes
Programming Languages
Curry A Tasty dish? Haskell Curry!.
Computer Science 312 I/O and Side Effects 1.
Introduction to Computer Science
Chapter 17 JavaScript Arrays
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Haskell Dealing with impurity 29-May-19.
Haskell Dealing with impurity 28-Jun-19.
Class code for pythonroom.com cchsp2cs
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Text Files and Random Numbers Computer Science 312 Text Files and Random Numbers 1

Isolate the Pure from the Impure External world (files, I/O devices, network ports, etc.) Impure Impure code Pure code

Text File Input with readFile Like Python’s open and read combined: Prelude> :type readFile readFile :: FilePath -> IO String Prelude> readFile "threelines.txt" "There are three\nlines of text\nin this file." Prelude> text <- readFile "threelines.txt" Prelude> putStrLn text There are three lines of text in this file. Bring in all the text in a single string, then extract what you need

Text File Output with writeFile Like Python’s open and write combined: Prelude> :type writeFile writeFile :: FilePath -> String -> IO () Prelude> let text = "There are three\nlines of text\nin this file." Prelude> writeFile "threelines.txt" text Send out the text in a single string

Numbering Lines Package text processing functions in a separate module {- File: TextUtilities.hs -} module TextUtilities (numberLines) where import Data.String (lines, unlines) numberLines :: String -> String numberLines text = unlines (helper (lines text) 1) where helper :: [String] -> Int -> [String] helper [] _ = [] helper (x:xs) count = (show count ++ " " ++ x) : helper xs (count + 1) Package text processing functions in a separate module

Numbering Lines The main app interacts with the outside world {- File: NumberLines.hs -} module Main (main) where import TextUtilities (numberLines) import TerminalIO (getString) main :: IO () main = do inPathName <- getString "Enter the input file name: " outPathName <- getString "Enter the output file name: " if inPathName == outPathName then do putStrLn "Error: files must have different names" return () else do inputText <- readFile inPathName let outputText = numberLines inputText writeFile outPathName outputText The main app interacts with the outside world

Random Numbers Needed to introduce non-determinism in programs Game-playing programs (dice, etc.) Guess the number Generate sentences, the Doctor program, etc. Cannot be a pure function!

Using MyRandom.randInt Prelude> :load MyRandom *MyRandom> randomInt 5 -- Returns a number between 0 and 4 2 *MyRandom> randomInt 5 -- Same argument, different value *MyRandom> randomInt 5 *MyRandom> testRandomInt 5 6 -- Check the randomness 4 3

Strategy for Randomness Base the number on the current time, obtained from the system clock Import the Data.Time.Clock module and the Data.Time.Format module Use the functions getCurrentTime and defaultTimeLocale to derive a big integer, which will be pseudo-random enough

Defining MyRandom.randInt {- File: MyRandom.hs -} module MyRandom where import Data.Time.Clock import Data.Time.Format -- Monadic function returns a random number between -- 0 and n - 1, inclusive. randomInt :: Int -> IO Int randomInt n = do time <- getCurrentTime return ((`rem` n) $ read $ take 6 $ formatTime defaultTimeLocale "%q" time)

A Probability Function *MyRandom> probability 0.75 True False *MyRandom> probability 0.25 Returns True a certain percentage of the time

A Probability Function *MyRandom> probability 0.75 True False *MyRandom> probability 0.25 probability :: Float -> IO Bool probability probFactor = do let probRange = round (100 * probFactor) randomValue <- randomInt 100 return (randomValue > 100 - probRange)

Generating Random Sentences Prelude> :load Generator *Generator> sentence "the table jumped the field beside a girl" *Generator> testGenerator 5 a cake dragged a girl below a chair the girl threw a table to the cake the chair hit a table to a cake the boy ate the dog to the chair a bat threw a dog below a chair

A Simple EBNF Grammar sentence = nounPhrase verbPhrase nounPhrase = article noun verbPhrase = verb nounPhrase prepositionalPhrase prepositionalPhrase = preposition nounPhrase A set of replacement rules for deriving sentences in a language Parts of speech or lexical categories, like noun and verb, are taken from sets of words in the language

The Vocabulary for Lexical Items nouns :: [String] nouns = ["bat", "boy", "girl", "dog", "cat", "chair", "fence", "table", "computer", "cake", "field"] verbs :: [String] verbs = ["hit", "threw", "pushed", "ate", "dragged", "jumped"] prepositions :: [String] prepositions = ["with", "to", "from", "on", "below", "above", "beside"] articles :: [String] articles = ["a", "the"] Parts of speech or lexical categories, like noun and verb, are taken from sets of words in the language

Like Python’s random.choice -- Returns a random item from a list. pickRandom :: [a] -> IO a pickRandom list = do index <- randomInt (length list) return (list !! index) Defined along with randomInt in the MyRandom module

Each Grammar Rule Gets a Function -- sentence = nounPhrase verbPhrase sentence :: IO String sentence = do np <- nounPhrase vp <- verbPhrase return (np ++ " " ++ vp) -- nounPhrase = article noun nounPhrase :: IO String nounPhrase = do article <- pickRandom articles noun <- pickRandom nouns return (article ++ " " ++ noun) --Etc.

Using Probabilities -- verbPhrase = nounPhrase verb [ prepositionalPhrase] verbPhrase :: IO String verbPhrase = do np <- nounPhrase verb <- pickRandom verbs let phrase = np ++ " " ++ verb ppOkay <- probability 0.25 if ppOkay then do pp = <- prepositionalPhrase return (phrase ++ " " ++ pp) else return phrase

Using Probabilities – The Doctor {- Generates and returns replies to user’s sentences. The current options are to change persons and prepend an interrogatory qualifier or just hedge. -} reply :: String -> IO String reply sentence = do hedgeOkay <- probability 0.33 if hedgeOkay then do hedge <- pickRandom hedges return hedge else do let newSentence = changePerson sentence qualifier <- pickRandom qualifiers return (qualifier ++ newSentence ++ "?")

Read the article on Erlang by Joe Armstrong (in Resources on Sakai) For next time Read the article on Erlang by Joe Armstrong (in Resources on Sakai)