-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
416 additions
and
278 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
PrimeraParte/wcount/examples/* | ||
## Literate haskell auxiliary files: | ||
*.hi | ||
*.o | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- Esto es un comentario | ||
|
||
powerLevel = 9001 | ||
|
||
over9000 = powerLevel > 9000 | ||
|
||
hola = "Hola" | ||
adios = "Adiós" | ||
|
||
holaMundo = (if over9000 then hola else adios) <> ", 🌍 !" | ||
|
||
main = putStrLn holaMundo |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
powerLevel :: Int | ||
powerLevel = 9001 | ||
|
||
over9000 :: Bool | ||
over9000 = powerLevel > 9000 | ||
|
||
holaMundo :: String | ||
holaMundo = (if over9000 then "Hola" else "Adiós") <> ", 🌍!" | ||
|
||
brillicos :: Char | ||
brillicos = '✨' | ||
|
||
main :: IO () | ||
main = putStrLn holaMundo | ||
|
||
e :: Double | ||
e = exp 1 | ||
|
||
-- ¿-(x+3) o -x+3? | ||
f :: Int -> Int | ||
f x = negate x + 3 | ||
|
||
-- Podemos especificar o no el tipo de nand | ||
nand :: Bool -> Bool -> Bool | ||
nand x y = not (x && y) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- :t id? | ||
id' x = x | ||
|
||
const' :: a -> b -> a | ||
const' x y = x | ||
|
||
fst' :: (a,b) -> a | ||
fst' (x,y) = x | ||
|
||
swap (x,y) = (y,x) | ||
|
||
|
||
-- shimweasel.com/2015/02/17/typed-holes-for-beginners | ||
g :: (a -> b) -> (a,c) -> (c,b) | ||
g x y = _resultado |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{- Reconocimiento de patrones -} | ||
|
||
not' :: Bool -> Bool | ||
not' True = False | ||
not' False = True | ||
|
||
-- :t ifThenElse? | ||
ifThenElse True x _ = x | ||
ifThenElse False _ y = y | ||
|
||
-- El orden es importante | ||
esNulo :: Int -> Bool | ||
esNulo 0 = True | ||
esNulo _ = False -- _ cuando no usamos el argumento | ||
|
||
factorialErroneo :: Int -> Int | ||
factorialErroneo n = n * factorialErroneo (n - 1) | ||
factorialErroneo 0 = 1 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
suma5 :: Int -> Int | ||
suma5 = (5+) | ||
|
||
mult4 :: Int -> Int | ||
mult4 = (4*) | ||
|
||
-- | ||
g :: Int -> Int | ||
g = suma5 . mult4 -- g x = 4*x + 5 |
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
l1 :: [Int] | ||
l1 = [1,2,3] | ||
|
||
l2 :: [Int] | ||
l2 = l1 <> [4,5] -- Concatenar | ||
|
||
l3 :: [Int] | ||
l3 = 0 : l2 -- Anteponer un elemento | ||
-- l3 == 0:(1:(2:(3:(4:(5:[]))))) | ||
|
||
-- :t l4 ? | ||
l4 = [] | ||
|
||
{- Funciones sobre listas -} | ||
|
||
head' :: [a] -> a | ||
-- Primer elemento | ||
head' [] = error "Lista vacía" | ||
head' (x:xs) = _head | ||
|
||
length' :: [a] -> Int | ||
-- Longitud | ||
-- ¿Por qué es ineficiente? | ||
length' [] = 0 | ||
length' (x:xs) = 1 + length' xs | ||
|
||
repeat' :: a -> [a] | ||
repeat' a = a : repeat' a | ||
|
||
(!!!) :: [a] -> Int -> a | ||
[] !!! _ = error "Índice demasiado grande" | ||
(x:_) !!! 0 = x | ||
(_:xs) !!! n = xs !!! (n-1) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
flip' :: (a -> b -> c) -> b -> a -> c | ||
flip' f y x = f x y | ||
|
||
|
||
ifThenElse' :: Bool -> a -> a -> a | ||
ifThenElse' True = _ifTrue | ||
ifThenElse' False = _ifFalse | ||
|
||
|
||
-- map f [a1, a2, ..., an] = | ||
-- [f a1, f a2, ..., f an] | ||
map' :: (a -> b) -> [a] -> [b] | ||
map' f [] = [] | ||
map' f (x:xs) = f x : map f xs | ||
|
||
suma1 :: [Int] -> [Int] | ||
suma1 = map (+1) | ||
|
||
exclama :: [String] -> [String] | ||
exclama = map (<>"!") | ||
|
||
|
||
-- foldr (⊕) z [a1,...,an] = | ||
-- a1 ⊕ (a2 ⊕ (... ⊕ (an ⊕ z))) | ||
foldr' :: (a -> b -> b) -> b -> [a] -> b | ||
foldr' g z [] = z | ||
foldr' g z (x:xs) = x `g` foldr' g z xs | ||
|
||
sum' :: [Int] -> Int | ||
sum' = foldr (+) 0 | ||
|
||
concat' :: [[a]] -> [a] | ||
concat' = foldr (<>) [] | ||
|
||
--- Hueco en el tipo | ||
filter' :: _ -> [a] -> [a] | ||
filter' p [] = [] | ||
filter' p (x:xs) = if p x then x:ys else ys | ||
where ys = filter' p xs |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- foldr (⊕) z [a1,...,an] = | ||
-- a1 ⊕ (a2 ⊕ (... ⊕ (an ⊕ z))) | ||
-- catamorphism | ||
|
||
map'' :: (a -> b) -> [a] -> [b] | ||
map'' f = foldr _gMap _zMap | ||
|
||
filter'' :: (a -> Bool) -> [a] -> [a] | ||
filter'' p = foldr _gFilter _zFilter | ||
|
||
length' :: [a] -> Int | ||
length' = foldr _gLength _zLength | ||
|
||
-- Y más! Ver ejercicios |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
{- Definición de tipos -} | ||
|
||
-- data Bool = False | True | ||
-- data Int = | ||
-- -9223372036854775808|...|-1|0|1|...|9223372036854775807 | ||
|
||
|
||
-- Tipos enumerados | ||
data Forma = Triangulo | Cuadrado | Circulo -- :t Triangulo ? | ||
|
||
esPoligono :: Forma -> Bool | ||
esPoligono Triangulo = True | ||
esPoligono Cuadrado = True | ||
esPoligono Circulo = False | ||
|
||
|
||
-- Tipos con campos :t P ? | ||
data Persona = P String Int | ||
|
||
-- Obtener la edad de una persona | ||
getEdad :: Persona -> Int | ||
getEdad (P _ edad) = edad | ||
|
||
-- Tienen la misma edad? | ||
mismaEdad :: Persona -> Persona -> Bool | ||
mismaEdad (P _ e1) (P _ e2) = e1 == e2 | ||
|
||
|
||
|
||
-- Varios constructores de datos | ||
data Color = RGB Double Double Double | ||
| HSV Double Double Double | ||
|
||
|
||
|
||
-- Tipos recursivos | ||
data Nat = Z | S Nat | ||
deriving (Eq,Show) | ||
|
||
suma :: Nat -> Nat -> Nat | ||
-- suma de naturales | ||
suma Z n = n | ||
suma (S n) m = S (suma n m) | ||
|
||
toInt :: Nat -> Int | ||
-- Pasar a entero | ||
toInt Z = 0 | ||
toInt (S n) = 1 + toInt n | ||
|
||
|
||
data Quizas a = Nada | Algo a | ||
deriving (Eq,Show) | ||
|
||
mapList :: (a -> b) -> Quizas a -> Quizas b | ||
mapList f Nada = Nada | ||
mapList f (Algo x) = Algo (f x) | ||
|
||
|
||
data Pareja a b = Pareja a b | ||
deriving (Eq, Show) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{- No podemos demostrar que las instancias cumplen las propiedades-} | ||
|
||
{- | ||
Clase de los monoides | ||
Sujeto a las propiedades: | ||
· x <+> (y <+> z) = (x <+> y) <+> z | ||
· x <+> neutro = neutro <+> x = x | ||
-} | ||
|
||
class Monoide a where | ||
(<+>) :: a -> a -> a | ||
neutro :: a | ||
|
||
instance Monoide [a] where | ||
xs <+> ys = xs <> ys | ||
neutro = [] | ||
|
||
instance Monoide Int where | ||
x <+> y = x + y | ||
neutro = 0 | ||
|
||
|
||
reduce :: (Monoide a) => [a] -> a | ||
reduce = foldr (<+>) neutro | ||
|
||
|
||
data Tree a = Empty | Node a (Tree a) (Tree a) | ||
deriving (Eq, Show) | ||
|
||
|
||
{- | ||
Clase de los funtores. | ||
Sujeto a las propiedades: | ||
· fmap id == id | ||
· fmap f . fmap g == fmap (f . g) | ||
-} | ||
class Funtor f where | ||
fmap' :: (a -> b) -> f a -> f b | ||
|
||
|
||
instance Funtor [] where | ||
fmap' f [] = [] | ||
fmap' f (x:xs) = f x : fmap' f xs | ||
|
||
|
||
instance Funtor Tree where | ||
fmap' f Empty = Empty | ||
fmap' f (Node a tLeft tRight) = Node (f a) (fmap' f tLeft) (fmap' f tRight) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{-#LANGUAGE DeriveFunctor, DeriveFoldable #-} | ||
|
||
data Tree a = Empty | Node a (Tree a) (Tree a) | ||
deriving (Eq, Show, Functor, Foldable) | ||
|
||
|
||
singleton x = Node x Empty Empty | ||
|
||
arbol :: Int -> Tree Int | ||
arbol 0 = singleton 1 | ||
arbol n = Node 1 subarbol subarbol | ||
where subarbol = fmap (2*) (arbol (n-1)) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.