CS302: Paradigms of Programming Functional Programming with Haskell - - PowerPoint PPT Presentation

cs302 paradigms of programming functional programming
SMART_READER_LITE
LIVE PREVIEW

CS302: Paradigms of Programming Functional Programming with Haskell - - PowerPoint PPT Presentation

CS302: Paradigms of Programming Functional Programming with Haskell Manas Thakur Spring 2020 Last Two Weeks of PoP Culmination of whatever we have been learning Recall this slide? 2 Whats a function? A map from values in a


slide-1
SLIDE 1

CS302: Paradigms of Programming Functional Programming with Haskell

Manas Thakur

Spring 2020

slide-2
SLIDE 2

Last Two Weeks of PoP

  • Culmination of whatever we have been learning
  • Recall this slide?

2

slide-3
SLIDE 3

What’s a function?

  • A map from values in a domain D to values in a range R.

3

f :: D -> R

  • Examples:

sin :: Float -> Float age :: Person -> Int add :: (Integer,Integer) -> Integer

  • Applying a function:

f(x) sin OR sin( )? Haskell: f x

θ θ

slide-4
SLIDE 4

Function composition

4

f :: Y -> Z g :: X -> Y

  • What is f . g?

f . g :: X -> Z (f . g) x :: f (g x)

  • All this is Haskell code!
slide-5
SLIDE 5

5

haskell.org

slide-6
SLIDE 6

Common Words

6

the: 154

  • f : 50

a : 18 and: 12 in : 11 … … n entries

slide-7
SLIDE 7

Type declaration

7

  • What’s the type of input?
  • An integer
  • A list of characters
  • What’s the type of output?
  • List of characters again!
  • What’s the type of commonWords?

Int, [Char] [Char] commonWords :: Int -> ([Char] -> [Char])

Functions in Haskell are curried!

slide-8
SLIDE 8

Possible algorithm

  • Get words from text
  • Count occurrence of each word
  • Sort in decreasing order
  • Display

8

slide-9
SLIDE 9

Type synonyms

9

words :: [Char] -> [[Char]] type Text = [Char] type Word = [Char] words :: Text -> [Word]

  • Get words from text
slide-10
SLIDE 10

Higher order functions

  • Should “The” and “the” be counted as same or different?
  • Solution: Convert each letter to lowercase!
  • Can you identify a function just by looking at its type signature?

10

What? :: (a -> b) -> [a] -> [b] map :: (a -> b) -> [a] -> [b] map toLower :: Text -> Text

Currying again!

slide-11
SLIDE 11

One of the most remarkable ideas in programming

11

sortWords [“to”, “be”, “or”, “not”, “to”, “be”] = [“be”, “be”, “not”, “or”, “to”, “to”] sortWords :: [Word] -> [Word]

  • Count occurrence of each word
  • Sort first?
  • Count adjacent runs of each word:

countRuns [“be”, “be”, “not”, “or”, “to”, “to”] = [(2,“be”), (1,“not”), (1,“or”), (2,“to”)] countRuns :: [Word] -> [(Int,Word)]

slide-12
SLIDE 12

I told you sorting is remarkable

  • Sort in decreasing order:

12

sortRuns [(2,“be”), (1,“not”), (1,“or”), (2,“to”)] = [(2,“be”), (2,“to”), (1,“not”), (1,“or”)] sortRuns :: [(Int,Word)] -> [(Int,Word)]

  • But we wanted to take only first n entries:

take 2 [(2,“be”), (2,“to”), (1,“not”), (1,“or”)] = [(2,“be”), (2,“to”)] take :: Int -> [a] -> [a]

Reminder: Every function is curried by default in Haskell!

slide-13
SLIDE 13

I only want to see the output

13

showRun :: (Int,Word) -> String showRun (2,”be”) = “be: 2\n”

  • We want to do this for each entry in the list:

map showRun :: [(Int,Word)] -> [String] String is a type synonym for [Char].

  • Finally, concatenate the list of Strings into one:

concat :: [[a]] -> [a]

slide-14
SLIDE 14

Common Words in a Nutshell

14

commonWords :: Int -> Text -> String commonWords n = concat . map showRun . take n . sortRuns . countRuns . sortWords . words . map toLower

  • This is how typical Haskell programs look!!
  • Mathematics is definitely something you didn’t study unnecessarily

in school!

  • But none of those functions was defined?
  • Some of them are predefined (concat, words, take, map, toLower).
  • Rest (showRun, sortRuns, countRuns, sortWords) we can define.
  • Does the implementation matter much? :-)