Difference between revisions of "FP Laboratory 5"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 6: Line 6:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
oddList :: Int -> Int -> [Int]
 +
oddList a b = [ x |x<-[a..b], odd x]
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 15: Line 17:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
import Data.Char
 +
 +
removeAllUpper :: String -> String
 +
removeAllUpper xs = [ x |x<-xs, not (isUpper x)]
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 27: Line 33:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<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>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 42: Line 53:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<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>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 55: Line 79:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
isPrime :: Int -> Bool
 +
isPrime n = null [x |x<-[2..ceiling (sqrt (fromIntegral n)::Double)], n `mod` x == 0]
 +
 +
goldbach :: Int-> [(Int, Int)]
 +
goldbach n = let primes = [x |x<-[2..(n `div` 2)+1], isPrime x]
 +
            in [(x,n-x) |x<-primes, isPrime (n-x)]
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 68: Line 98:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
isPrime :: Int -> Bool
 +
isPrime n = null [x |x<-[2..ceiling (sqrt (fromIntegral n)::Double)], n `mod` x == 0]
 +
 +
goldbach :: Int-> [(Int, Int)]
 +
goldbach n = let primes = [x |x<-[2..(n `div` 2)+1], isPrime x]
 +
            in [(x,n-x) |x<-primes, isPrime (n-x)]
 +
 +
goldbachList :: Int -> Int-> Int -> [(Int, Int)]
 +
goldbachList a b limit = filter (\(x,_)-> x>limit) [head (goldbach x) | x<-[a..b], even x]
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 81: Line 120:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<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>
 
</syntaxhighlight>
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>

Revision as of 09:33, 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]
oddList :: Int -> Int -> [Int]
oddList a b = [ x |x<-[a..b], odd x]
  • Create a function that removes all upper case letters from a string.
removeAllUpper :: String -> String
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]
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]
  • 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)]
isPrime :: Int -> Bool
isPrime n = null [x |x<-[2..ceiling (sqrt (fromIntegral n)::Double)], n `mod` x == 0]

goldbach :: Int-> [(Int, Int)]
goldbach n = let primes = [x |x<-[2..(n `div` 2)+1], isPrime x]
             in [(x,n-x) |x<-primes, isPrime (n-x)]
  • 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)]
isPrime :: Int -> Bool
isPrime n = null [x |x<-[2..ceiling (sqrt (fromIntegral n)::Double)], n `mod` x == 0]

goldbach :: Int-> [(Int, Int)]
goldbach n = let primes = [x |x<-[2..(n `div` 2)+1], isPrime x]
             in [(x,n-x) |x<-primes, isPrime (n-x)]

goldbachList :: Int -> Int-> Int -> [(Int, Int)]
goldbachList a b limit = filter (\(x,_)-> x>limit) [head (goldbach x) | x<-[a..b], even x]
  • 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",...]
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)