Difference between revisions of "FP Laboratory 5"
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
* Create a function that generates a list of all odd numbers in given interval. | * 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">oddList :: Int -> Int -> [Int]</syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* Create a function that removes all upper case letters from a string. | * Create a function that removes all upper case letters from a string. | ||
<syntaxhighlight lang="Haskell">removeAllUpper :: String -> String</syntaxhighlight> | <syntaxhighlight lang="Haskell">removeAllUpper :: String -> String</syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* Create functions that computes union and intersection of two sets. | * Create functions that computes union and intersection of two sets. | ||
<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||
Line 10: | Line 24: | ||
intersection :: Eq a => [a] -> [a] -> [a] | intersection :: Eq a => [a] -> [a] -> [a] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
== More complex functions == | == More complex functions == | ||
Line 19: | Line 39: | ||
[('h',3),('e',3),('l',6),('o',3),(' ',2)] | [('h',3),('e',3),('l',6),('o',3),(' ',2)] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case yet. Create a function, that computes for a given even integer number the list of pairs of primes, that satisfies the rule of Goldbach's conjecture. | * Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case yet. Create a function, that computes for a given even integer number the list of pairs of primes, that satisfies the rule of Goldbach's conjecture. | ||
<syntaxhighlight lang="Haskell">goldbach :: Int-> [(Int, Int)]</syntaxhighlight> | <syntaxhighlight lang="Haskell">goldbach :: Int-> [(Int, Int)]</syntaxhighlight> | ||
Line 25: | Line 52: | ||
[(5, 23),(11,17)] | [(5, 23),(11,17)] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. We will be searching for cases that violates this rule. Create a function, that has three parameters. First two defines an interval, where we will be searching for Goldbach numbers. The last parameter is the limit. For each number in this interval, find Goldbach's pair with smallest prime number. If this smallest number is bigger than given limit, the corresponding pair will be in the result. | * In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. We will be searching for cases that violates this rule. Create a function, that has three parameters. First two defines an interval, where we will be searching for Goldbach numbers. The last parameter is the limit. For each number in this interval, find Goldbach's pair with smallest prime number. If this smallest number is bigger than given limit, the corresponding pair will be in the result. | ||
<syntaxhighlight lang="Haskell">goldbachList :: Int -> Int-> Int -> [(Int, Int)]</syntaxhighlight> | <syntaxhighlight lang="Haskell">goldbachList :: Int -> Int-> Int -> [(Int, Int)]</syntaxhighlight> | ||
Line 31: | Line 65: | ||
[(73,919),(61,1321),(67,1789),(61,1867)] | [(73,919),(61,1321),(67,1789),(61,1867)] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* Create a function that generates all combinations of given length from the characters from given string. | * Create a function that generates all combinations of given length from the characters from given string. | ||
<syntaxhighlight lang="Haskell">combinations :: Int -> String -> [String]</syntaxhighlight> | <syntaxhighlight lang="Haskell">combinations :: Int -> String -> [String]</syntaxhighlight> | ||
Line 37: | Line 78: | ||
["abc","abd","abe",...] | ["abc","abd","abe",...] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> |
Revision as of 09:26, 24 September 2020
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]
- Create a function that removes all upper case letters from a string.
removeAllUpper :: String -> String
- Create functions that computes union and intersection of two sets.
union :: Eq a => [a] -> [a] -> [a]
intersection :: Eq a => [a] -> [a] -> [a]
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)]
- Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers. Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case yet. Create a function, that computes for a given even integer number the list of pairs of primes, that satisfies the rule of Goldbach's conjecture.
goldbach :: Int-> [(Int, Int)]
*Main>goldbach 28
[(5, 23),(11,17)]
- In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. We will be searching for cases that violates this rule. Create a function, that has three parameters. First two defines an interval, where we will be searching for Goldbach numbers. The last parameter is the limit. For each number in this interval, find Goldbach's pair with smallest prime number. If this smallest number is bigger than given limit, the corresponding pair will be in the result.
goldbachList :: Int -> Int-> Int -> [(Int, Int)]
*Main>goldbachList 4 2000 50
[(73,919),(61,1321),(67,1789),(61,1867)]
- Create a function that generates all combinations of given length from the characters from given string.
combinations :: Int -> String -> [String]
*Main> combinations 3 "abcdef"
["abc","abd","abe",...]