Difference between revisions of "FP Laboratory 5"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 30: Line 30:
 
<syntaxhighlight lang="Haskell">goldbach-list:: Int-> Int -> (Int, Int)</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">goldbach-list:: Int-> Int -> (Int, Int)</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
*Main>goldbachList' 4 2000 50
+
*Main>goldbachList 4 2000 50
 
[(73,919),(61,1321),(67,1789),(61,1867)]
 
[(73,919),(61,1321),(67,1789),(61,1867)]
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:31, 17 September 2019

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 takes a string and converts all characters to upper case letters.
allToUpper :: String -> String
  • 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 :: [a] -> [a] -> [a]
intersection :: [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. Create a function, that has three parameters. First two are an interval, where we will be searching for Goldbach numbers. The last parameter is the limit. If one of the elements in these pair is bigger than this limit, the corresponding pair will be in the result.
goldbach-list:: 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",...]