Difference between revisions of "FP Laboratory 5"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 20: Line 20:
 
*Main>countThem "hello hello hello"
 
*Main>countThem "hello hello hello"
 
[('h',3),('e',3),('l',6),('o',3),(' ',2)]
 
[('h',3),('e',3),('l',6),('o',3),(' ',2)]
 +
</syntaxhighlight>
 +
* 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" class="myDark">
 +
*Main> combinations 3 "abcdef"
 +
["abc","abd","abe",...]
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:15, 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)]
  • 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",...]