Programação Funcional
em Haskell
Resolução Lista 2
Um pouco sobre Dobras
 Função foldl, também chamada de "left fold" (dobra
esquerda), dobra a lista a partir do lado esquerdo.
 A função foldr dobra a lista a partir do lado direito
 As funções foldl1 e foldr1 funcionam da mesma forma que
foldl e foldr, só que você não precisa informar explicitamente
qual o valor inicial.
 Ele assume que o primeiro (ou último) elemento da lista é o
valor inicial da dobra e então inicia a dobra com ele.
Dobras em Haskell
foldr (+) 0 [1..10] valor inicial 0, 0 + 10 + ... 1
foldl (+) 0 [1..10] valor inicial 0, 0 + 0 + ... 10
foldr1 (+) [1..10] valor inical 10,10 + 9 + ... 1
foldl1 (+) [1..10] valor inical 1, 1 + 2 + ... 10
Dobras em Haskell
Prelude> putStrLn $ foldr (x y -> concat ["(",x,"+",y,")"]) "0" (map show
[1..13])
(1+(2+(3+(4+(5+(6+(7+(8+(9+(10+(11+(12+(13+0)))))))))))))
Prelude> putStrLn $ foldl (x y -> concat ["(",x,"+",y,")"]) "0" (map show
[1..13])
(((((((((((((0+1)+2)+3)+4)+5)+6)+7)+8)+9)+10)+11)+12)+13)
Prelude> putStrLn $ foldr1 (x y -> concat ["(",x,"+",y,")"]) (map show [1..13])
(1+(2+(3+(4+(5+(6+(7+(8+(9+(10+(11+(12+13))))))))))))
Prelude> putStrLn $ foldl1 (x y -> concat ["(",x,"+",y,")"]) (map show [1..13])
((((((((((((1+2)+3)+4)+5)+6)+7)+8)+9)+10)+11)+12)+13)
Dobras em Haskell
Prelude> foldr (x y->x:'.':y) "" []:"CRIS":[]
["",“CRIS"]
Prelude> foldr (x y->x:'.':y) "" "CRIS":[]
[“C.R.I.S."]
Prelude> foldr (x y->x:'.':y) "" "CRIS"
“C.R.I.S."
Prelude> foldr (x y->x:'.':y) "" [‘c']:“CRIS":[]
["a.",“CRIS"]
1° QUESTÃO
--1° NIVEL 3
Prelude> length (filter (/='a') "banana")
3
Prelude> length (filter (/='n') "banana")
4
Prelude> length ([1,2]:[3,4]:[])
2
Prelude> length [1,3..7]
4
Prelude> length (drop 6 "abcdefg123")
4
Prelude> length ([1,2]:[3]:[])
2
Prelude> length [(x,y)| x<-"abc", y<-[0,1,2]]
9
1° QUESTÃO
--1° NIVEL 3
Prelude> head [n | n<-[1..], n*n>15]
44
Prelude> head [n | n<-[1..], n*n>20]
5
Prelude> head([]++[1,2]++[3])
1
Prelude> [x | x<- "ab", n<-[1,2,3]]
"aaabbb"
Prelude> map (`div` 2) [1,2,3]
[0,1,1]
Prelude> (reverse [3,2])++ [4,5]
[2,3,4,5]
Prelude> reverse ([4,5]++[1,2,3])
[3,2,1,5,4]
1° QUESTÃO
--1° NIVEL 3
Prelude> :t ([1,2,3], "banana")
([1,2,3], "banana") :: Num t => ([t], [Char])
Prelude> :t [(1,"banana"), (2,"maçã")]
[(1,"banana"), (2,"maçã")] :: Num t => [(t, [Char])]
2° QUESTÃO
--2° NIVEL 5
Prelude> foldr (-) (-2) [1,2,3,4]
-4
Prelude> foldr (-) (2) [1,2,3,4]
0
Prelude> foldl (-) (2) [1,2,3,4]
-8
Prelude> foldl (-) (-2) [1,2,3,4]
-12
3° QUESTÃO
--3° NIVEL 6
Prelude> foldr (+) 2 (map (+1) [1,2,3])
11
Prelude> foldl (+) 2 (map (+1) [1,2,3])
11
Prelude> foldr (-) 2 (map ((-)1) [1,2,2])
-2
Prelude> foldl (-) 2 (map ((-)1) [1,2,2])
4
Prelude> foldr(x y->x:'.':y) "" "PAULO"
"P.A.U.L.O.“
Prelude> foldl(x y->x:'.':y) "" "ITALOS"
--ERRO
4° QUESTÃO
--4° NIVEL 7
Prelude> foldl (+) 0 (filter (>1) [sum(map ((-)1) [1,2,2])])
0
Prelude> sum(map (-1) [1,2,2])
--ERRO
Prelude> [map (>2) [1,2,3]] ++ ([]) ++ [[True]]
[[False,False,True],[True]]
Prelude> [map (>2) [1,6,6]] ++ ([]) ++ [[]]
[[False,True,True],[]]
Prelude> [map (>2) [-1, 2,4]] ++([])
[[False,False,True]]
5° QUESTÃO
--5° NIVEL MASTER PROIBIDO PARA MENORES DE 18 ANOS.
Prelude> foldr1 (-) [4,3,2]
3
Prelude> foldl1 (-) [-2,3,4]
-9
Agradecimentos
Ao Prof°- Ricardo Reis e a todos os participantes do projeto
haskell ufc.

Resolução lista2

  • 1.
  • 2.
    Um pouco sobreDobras  Função foldl, também chamada de "left fold" (dobra esquerda), dobra a lista a partir do lado esquerdo.  A função foldr dobra a lista a partir do lado direito  As funções foldl1 e foldr1 funcionam da mesma forma que foldl e foldr, só que você não precisa informar explicitamente qual o valor inicial.  Ele assume que o primeiro (ou último) elemento da lista é o valor inicial da dobra e então inicia a dobra com ele.
  • 3.
    Dobras em Haskell foldr(+) 0 [1..10] valor inicial 0, 0 + 10 + ... 1 foldl (+) 0 [1..10] valor inicial 0, 0 + 0 + ... 10 foldr1 (+) [1..10] valor inical 10,10 + 9 + ... 1 foldl1 (+) [1..10] valor inical 1, 1 + 2 + ... 10
  • 4.
    Dobras em Haskell Prelude>putStrLn $ foldr (x y -> concat ["(",x,"+",y,")"]) "0" (map show [1..13]) (1+(2+(3+(4+(5+(6+(7+(8+(9+(10+(11+(12+(13+0))))))))))))) Prelude> putStrLn $ foldl (x y -> concat ["(",x,"+",y,")"]) "0" (map show [1..13]) (((((((((((((0+1)+2)+3)+4)+5)+6)+7)+8)+9)+10)+11)+12)+13) Prelude> putStrLn $ foldr1 (x y -> concat ["(",x,"+",y,")"]) (map show [1..13]) (1+(2+(3+(4+(5+(6+(7+(8+(9+(10+(11+(12+13)))))))))))) Prelude> putStrLn $ foldl1 (x y -> concat ["(",x,"+",y,")"]) (map show [1..13]) ((((((((((((1+2)+3)+4)+5)+6)+7)+8)+9)+10)+11)+12)+13)
  • 5.
    Dobras em Haskell Prelude>foldr (x y->x:'.':y) "" []:"CRIS":[] ["",“CRIS"] Prelude> foldr (x y->x:'.':y) "" "CRIS":[] [“C.R.I.S."] Prelude> foldr (x y->x:'.':y) "" "CRIS" “C.R.I.S." Prelude> foldr (x y->x:'.':y) "" [‘c']:“CRIS":[] ["a.",“CRIS"]
  • 6.
    1° QUESTÃO --1° NIVEL3 Prelude> length (filter (/='a') "banana") 3 Prelude> length (filter (/='n') "banana") 4 Prelude> length ([1,2]:[3,4]:[]) 2 Prelude> length [1,3..7] 4 Prelude> length (drop 6 "abcdefg123") 4 Prelude> length ([1,2]:[3]:[]) 2 Prelude> length [(x,y)| x<-"abc", y<-[0,1,2]] 9
  • 7.
    1° QUESTÃO --1° NIVEL3 Prelude> head [n | n<-[1..], n*n>15] 44 Prelude> head [n | n<-[1..], n*n>20] 5 Prelude> head([]++[1,2]++[3]) 1 Prelude> [x | x<- "ab", n<-[1,2,3]] "aaabbb" Prelude> map (`div` 2) [1,2,3] [0,1,1] Prelude> (reverse [3,2])++ [4,5] [2,3,4,5] Prelude> reverse ([4,5]++[1,2,3]) [3,2,1,5,4]
  • 8.
    1° QUESTÃO --1° NIVEL3 Prelude> :t ([1,2,3], "banana") ([1,2,3], "banana") :: Num t => ([t], [Char]) Prelude> :t [(1,"banana"), (2,"maçã")] [(1,"banana"), (2,"maçã")] :: Num t => [(t, [Char])]
  • 9.
    2° QUESTÃO --2° NIVEL5 Prelude> foldr (-) (-2) [1,2,3,4] -4 Prelude> foldr (-) (2) [1,2,3,4] 0 Prelude> foldl (-) (2) [1,2,3,4] -8 Prelude> foldl (-) (-2) [1,2,3,4] -12
  • 10.
    3° QUESTÃO --3° NIVEL6 Prelude> foldr (+) 2 (map (+1) [1,2,3]) 11 Prelude> foldl (+) 2 (map (+1) [1,2,3]) 11 Prelude> foldr (-) 2 (map ((-)1) [1,2,2]) -2 Prelude> foldl (-) 2 (map ((-)1) [1,2,2]) 4 Prelude> foldr(x y->x:'.':y) "" "PAULO" "P.A.U.L.O.“ Prelude> foldl(x y->x:'.':y) "" "ITALOS" --ERRO
  • 11.
    4° QUESTÃO --4° NIVEL7 Prelude> foldl (+) 0 (filter (>1) [sum(map ((-)1) [1,2,2])]) 0 Prelude> sum(map (-1) [1,2,2]) --ERRO Prelude> [map (>2) [1,2,3]] ++ ([]) ++ [[True]] [[False,False,True],[True]] Prelude> [map (>2) [1,6,6]] ++ ([]) ++ [[]] [[False,True,True],[]] Prelude> [map (>2) [-1, 2,4]] ++([]) [[False,False,True]]
  • 12.
    5° QUESTÃO --5° NIVELMASTER PROIBIDO PARA MENORES DE 18 ANOS. Prelude> foldr1 (-) [4,3,2] 3 Prelude> foldl1 (-) [-2,3,4] -9
  • 13.
    Agradecimentos Ao Prof°- RicardoReis e a todos os participantes do projeto haskell ufc.