Created
January 23, 2018 18:06
-
-
Save tiagoad/33868c4b17c8821d2779d38407034e4e to your computer and use it in GitHub Desktop.
Exames de Princípios de Programação - FCUL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Test.QuickCheck | |
-- Grupo 1 | |
-- a) | |
encontra :: (a -> Bool) -> [a] -> Maybe a | |
encontra f xs | |
| length matches == 0 = Nothing | |
| otherwise = Just (head matches) | |
where | |
matches = filter f xs | |
-- b) | |
fatias :: Int -> [a] -> [[a]] | |
fatias n xs | |
| (length xs) < n = [xs] | |
| otherwise = (take n xs) : fatias n (drop n xs) | |
-- c) | |
separa :: Eq a => [a] -> [a] -> [[a]] | |
separa sep [] = [] | |
separa sep str = left : separa sep right | |
where | |
ok = (flip notElem) sep | |
partes = span ok str | |
left = fst partes | |
right = dropWhile (not . ok) (snd partes) | |
-- Grupo 2 | |
data Doc = Vazio | |
| Texto String | |
| NovaLinha | |
| Concat Doc Doc | |
-- a) | |
instance Show Doc where | |
show Vazio = "" | |
show NovaLinha = "\n" | |
show (Texto str) = str | |
show (Concat a b) = show a ++ show b | |
-- b) | |
instance Eq Doc where | |
a == b = (show a) == (show b) | |
-- Grupo 3 | |
-- a) | |
instance Arbitrary Doc where | |
arbitrary = oneof [ | |
return Vazio, | |
return NovaLinha, | |
do | |
t <- arbitrary :: Gen String | |
return $ Texto t, | |
do | |
a <- arbitrary | |
b <- arbitrary | |
return $ Concat a b] | |
-- b) | |
prop_concat_vazio doc = | |
Concat doc Vazio == doc && | |
Concat Vazio doc == doc | |
prop_concat_assoc a b c= | |
(Concat (Concat a b) c) == (Concat a (Concat b c)) | |
-- Grupo 4 | |
??? | |
-- Grupo 5 (Java) | |
Stream.iterate(0, x -> x + 1).limit(n) | |
.filter(x -> x%2 == 1) | |
.map(x -> x * x) | |
.reduce(0, (a, b) -> a + b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment