Difference between revisions of "PFP Laboratory 3"
Jump to navigation
Jump to search
(Created page with "== High-order functions == * Create a function that takes a string and converts all characters to upper case letters. <syntaxhighlight lang="Haskell">allToUpper :: String ->...") |
|||
Line 44: | Line 44: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
+ | == List comprehension == | ||
+ | Using the list comprehension implement following functions: | ||
+ | |||
+ | * Create a function that generates a list of all odd numbers in given interval. | ||
+ | <syntaxhighlight lang="Haskell">oddList :: Int -> Int -> [Int]</syntaxhighlight> | ||
+ | <syntaxhighlight lang="Haskell" class="myDark"> | ||
+ | *Main> oddList 1 10 | ||
+ | [1,3,5,7,9] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | oddList :: Int -> Int -> [Int] | ||
+ | oddList a b = [ x |x<-[a..b], odd x] | ||
+ | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/TXOOI93332]] | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
+ | * Create a function that removes all upper case letters from a string. | ||
+ | <syntaxhighlight lang="Haskell">removeAllUpper :: String -> String</syntaxhighlight> | ||
+ | <syntaxhighlight lang="Haskell" class="myDark"> | ||
+ | *Main> removeAllUpper "ABCabcABC" | ||
+ | "abc" | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | import Data.Char | ||
+ | |||
+ | removeAllUpper :: String -> String | ||
+ | removeAllUpper xs = [ x |x<-xs, not (isUpper x)] | ||
+ | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/TXOOI93332]] | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
+ | * Create functions that computes union and intersection of two sets. | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | union :: Eq a => [a] -> [a] -> [a] | ||
+ | intersection :: Eq a => [a] -> [a] -> [a] | ||
+ | </syntaxhighlight> | ||
+ | <syntaxhighlight lang="Haskell" class="myDark"> | ||
+ | *Main> union [1..5] [3..10] | ||
+ | [1,2,3,4,5,6,7,8,9,10] | ||
+ | *Main> intersection [1..5] [3..10] | ||
+ | [3,4,5] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | union :: Eq a => [a] -> [a] -> [a] | ||
+ | union xs ys = xs ++ [y| y<-ys, not (elem y xs)] | ||
+ | |||
+ | intersection :: Eq a => [a] -> [a] -> [a] | ||
+ | intersection xs ys = [y| y<-ys, elem y xs] | ||
+ | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/TXOOI93332]] | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
+ | == More complex functions == | ||
+ | * Create a function that count the number of occurrences of all characters from a given string. | ||
+ | |||
+ | <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/B2LFNJfC-TU]]</div> | ||
+ | <syntaxhighlight lang="Haskell">countThem :: String -> [(Char, Int)]</syntaxhighlight> | ||
+ | <syntaxhighlight lang="Haskell" class="myDark"> | ||
+ | *Main>countThem "hello hello hello" | ||
+ | [('h',3),('e',3),('l',6),('o',3),(' ',2)] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | unique :: String -> String | ||
+ | unique n = reverse(tmp n "") where | ||
+ | tmp [] store = store | ||
+ | tmp (x:xs) store | x `elem` store = tmp xs store | ||
+ | | otherwise = tmp xs (x:store) | ||
+ | |||
+ | unique' :: String -> String | ||
+ | unique' [] = [] | ||
+ | unique' (x:xs) = x: unique' (filter (/=x)xs) | ||
+ | |||
+ | countThem :: String -> [(Char, Int)] | ||
+ | countThem xs = let u = unique xs | ||
+ | in [(x, length (filter (==x) xs)) |x<-u] | ||
+ | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/UVITE26792]] | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
+ | * Create a function that generates all combinations of given length from the characters from given string. You can assume, that all character are unique and the given length is not bigger then the length of this string. | ||
+ | <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/-muPoJucLyI]]</div> | ||
+ | <syntaxhighlight lang="Haskell">combinations :: Int -> String -> [String]</syntaxhighlight> | ||
+ | <syntaxhighlight lang="Haskell" class="myDark"> | ||
+ | *Main> combinations 3 "abcdef" | ||
+ | ["abc","abd","abe",...] | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | combinations :: Int -> String -> [String] | ||
+ | combinations 1 xs = [[x]| x<-xs] | ||
+ | combinations n (x:xs) | n == length (x:xs) = [(x:xs)] | ||
+ | |otherwise = [[x] ++ y |y<-combinations (n-1) xs ] | ||
+ | ++ (combinations n xs) | ||
+ | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/RTRV90066]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> |
Revision as of 10:16, 29 September 2022
High-order functions
- Create a function that takes a string and converts all characters to upper case letters.
allToUpper :: String -> String
*Main> allToUpper "aAbc"
"AABC"
import Data.Char
allToUpper :: String -> String
allToUpper xs = [toUpper x |x<-xs]
allToUpper' :: String -> String
allToUpper' xs = map toUpper xs
- Implement the
quicksort
algorithm. As a pivot use always the first element in the list. For dividing the list, use the functionfilter
.
quicksort :: (Ord a) => [a] -> [a]
*Main> filter (<5) [1..10]
[1,2,3,4]
*Main> quicksort [1,5,3,7,9,5,2,1]
[1,1,2,3,5,5,7,9]
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = let lp = filter (< x) xs
rp = filter (>= x) xs
in quicksort lp ++ [x] ++ quicksort rp
List comprehension
Using the list comprehension implement following functions:
- Create a function that generates a list of all odd numbers in given interval.
oddList :: Int -> Int -> [Int]
*Main> oddList 1 10
[1,3,5,7,9]
- Create a function that removes all upper case letters from a string.
removeAllUpper :: String -> String
*Main> removeAllUpper "ABCabcABC"
"abc"
import Data.Char
removeAllUpper :: String -> String
removeAllUpper xs = [ x |x<-xs, not (isUpper x)]
- Create functions that computes union and intersection of two sets.
union :: Eq a => [a] -> [a] -> [a]
intersection :: Eq a => [a] -> [a] -> [a]
*Main> union [1..5] [3..10]
[1,2,3,4,5,6,7,8,9,10]
*Main> intersection [1..5] [3..10]
[3,4,5]
union :: Eq a => [a] -> [a] -> [a]
union xs ys = xs ++ [y| y<-ys, not (elem y xs)]
intersection :: Eq a => [a] -> [a] -> [a]
intersection xs ys = [y| y<-ys, elem y xs]
More complex functions
- Create a function that count the number of occurrences of all characters from a given string.
countThem :: String -> [(Char, Int)]
*Main>countThem "hello hello hello"
[('h',3),('e',3),('l',6),('o',3),(' ',2)]
unique :: String -> String
unique n = reverse(tmp n "") where
tmp [] store = store
tmp (x:xs) store | x `elem` store = tmp xs store
| otherwise = tmp xs (x:store)
unique' :: String -> String
unique' [] = []
unique' (x:xs) = x: unique' (filter (/=x)xs)
countThem :: String -> [(Char, Int)]
countThem xs = let u = unique xs
in [(x, length (filter (==x) xs)) |x<-u]
- Create a function that generates all combinations of given length from the characters from given string. You can assume, that all character are unique and the given length is not bigger then the length of this string.
combinations :: Int -> String -> [String]
*Main> combinations 3 "abcdef"
["abc","abd","abe",...]