Difference between revisions of "FP Laboratory 4"
Jump to navigation
Jump to search
Line 16: | Line 16: | ||
take' n (x:xs) = x: take' (n-1) xs | take' n (x:xs) = x: take' (n-1) xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 33: | Line 34: | ||
drop' n (_:xs) = drop' (n-1) xs | drop' n (_:xs) = drop' (n-1) xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 50: | Line 52: | ||
| otherwise = minimum' (y:z) | | otherwise = minimum' (y:z) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 74: | Line 77: | ||
divisors'' n = [x | x<-[1..n], n `mod` x == 0] | divisors'' n = [x | x<-[1..n], n `mod` x == 0] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 93: | Line 97: | ||
zipThem _ _ = [] | zipThem _ _ = [] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 119: | Line 124: | ||
(concat (replicate (length x) y)) | (concat (replicate (length x) y)) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 137: | Line 143: | ||
tmp x = fibStep (tmp (x-1)) | tmp x = fibStep (tmp (x-1)) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 158: | Line 165: | ||
allToUpper' xs = map toUpper xs | allToUpper' xs = map toUpper xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 180: | Line 188: | ||
in quicksort lp ++ [x] ++ quicksort rp | in quicksort lp ++ [x] ++ quicksort rp | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BVU17842]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> |
Revision as of 12:48, 22 September 2021
Functions working with lists
Implement following functions:
- Create a function that takes first n elements of the list.
take' :: Int -> [a] -> [a]
*Main> take' 2 [1,2,3]
[1,2]
- Create a function that takes the remaining list after the first n elements.
drop' :: Int -> [a] -> [a]
*Main> drop' 2 [1,2,3]
[3]
- Create a function that find the smallest element in the list. Consider input restrictions.
minimum' :: [a] -> a -- Is this right?
*Main> minimum' [1,3,4,0]
0
minimum' :: Ord a => [a] -> a
minimum' [x] = x
minimum' (x:y:z) | x < y = minimum' (x:z)
| otherwise = minimum' (y:z)
- Find all integer divisors of a given number.
divisors :: Int -> [Int]
*Main> divisors 32
[1,2,4,8,16,32]
divisors :: Int -> [Int]
divisors n = tmp n where
tmp 0 = []
tmp x | n `mod` x == 0 = x: tmp (x-1)
| otherwise = tmp (x-1)
divisors' :: Int -> [Int]
divisors' n = filter (\x -> n `mod` x == 0) [1..n]
divisors'' :: Int -> [Int]
divisors'' n = [x | x<-[1..n], n `mod` x == 0]
Functions working with lists and tuples
Implement following functions:
- Create a function that merge two lists into one list of tuples.
zipThem:: [a] -> [b] -> [(a,b)]
*Main> zipThem [1,2,3] "ABCD"
[(1,'A'),(2,'B'),(3,'C')]
- Create a function that compute Cartesian product of two vectors.
dotProduct :: [a] -> [b] -> [(a,b)]
*Main> dotProduct [1..4] "ABC"
[(1,'A'),(1,'B'),(1,'C'),(2,'A'),(2,'B'),(2,'C'),(3,'A'),(3,'B'),(3,'C'),(4,'A'),(4,'B'),(4,'C')]
dotProduct :: [a] -> [b] -> [(a,b)]
dotProduct [] _ = []
dotProduct (x:xs) ys = tmp ys ++ dotProduct xs ys where
tmp [] = []
tmp (b:bs) = (x,b) : tmp bs
dotProduct' :: [a] -> [b] -> [(a,b)]
dotProduct' xs ys = [(x,y)|x<-xs, y<-ys]
dotProduct'' :: [a] -> [b] -> [(a,b)]
dotProduct'' x y =
zip (concat (map (replicate (length y)) x))
(concat (replicate (length x) y))
- Create a function that computes n-th number in the Fibonacci sequence. The function should use tuples in the solution.
fibonacci :: Int -> Int
*Main> fibonacci 12
144
fibonacci :: Int -> Int
fibonacci n = fst (tmp n) where
fibStep (a,b) = (b,a+b)
tmp 0 = (0,1)
tmp x = fibStep (tmp (x-1))
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]