Created
August 23, 2020 19:24
-
-
Save juanbono/fbde4a7723b0d425199bae13b0c9b1a4 to your computer and use it in GitHub Desktop.
Clase 2 del Paradigma Funcional sobre tipos de datos (tuplas, listas y records)
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
-- Tipos de Datos | |
-- | |
-- Tuplas | |
-- | |
pos = (1, 2) | |
tercero (a, b, c) = c | |
cuarto (_, _, _, d) = d | |
-- 2 formas de escribir funciones usando tuplas | |
-- accediendo a las componentes usando funciones existentes | |
-- (fst y snd) | |
suma tupla = fst tupla + snd tupla | |
-- matcheando directamente en la estructura de la tupla | |
suma2 (x, y) = x + y | |
-- tambien se puede devolver tuplas | |
duplicar x = (x, x) | |
-- listas | |
-- algunos usos | |
-- length [1,2,4] | |
-- head [1,2,3,4] | |
-- last [1,2,3,4] | |
-- estructura de una lista | |
-- [] : lista vacia | |
-- (x:xs) : (cabeza:cola) | |
sumatoria [] = 0 | |
sumatoria (x:xs) = x + sumatoria xs | |
-- sumatoria [1,2,3] = 1 + sumatoria [2,3] | |
-- = 1 + (2 + sumatoria [3]) | |
-- = 1 + (2 + (3 + sumatoria [])) | |
-- = 1 + 2 + 3 + 0 | |
-- = 6 | |
-- [3] == (3:[]) | |
productoria [] = 0 | |
productoria (x:xs) = x * productoria xs | |
-- practicando matcheos en listas | |
sumarPrimeros2 [] = 0 | |
sumarPrimeros2 [x] = x | |
sumarPrimeros2 (primero:(segundo:elResto)) = primero + segundo | |
-- funciones varias sobre listas | |
-- primer elemento => head lista | |
-- head [True, False] ---> True | |
-- tail [1,2,3,4,5] -----> [2,3,4,5] | |
-- sumatoria de una lista de numeros usando head y tail | |
sumatoriaSinPatrones [] = 0 | |
sumatoriaSinPatrones lista = head lista + sumatoriaSinPatrones (tail lista) | |
sumarListas [] [] = [] | |
sumarListas [] (x:xs) = [] | |
sumarListas (y:ys) [] = [] | |
sumarListas (y:ys) (x:xs) = (x + y) : sumarListas ys xs | |
-- sumarListas [1,2,3,4] [1,2,3,4] devuelve [2, 4, 6, 8] | |
sumarLargoDeListas lista1 lista2 = length lista1 + length lista2 | |
-- ("marcos", [("chocolate", 200)]) | |
data Ingrediente = Ing String | |
deriving Show | |
data Pastelero = UnPastelero String [Ingrediente] | |
deriving Show | |
-- marcosData = UnPastelero "marcos" ["chocolate"] | |
marcosData = UnPastelero "marcos" [Ing "chocolate"] | |
nombre (UnPastelero nombre _) = nombre | |
ingredientes (UnPastelero _ ingredientes) = ingredientes | |
-- usamos todos los ingredientes del pastelero | |
usarTodosLosIng (UnPastelero nombre ingredientes) = UnPastelero nombre [] | |
-- Record | |
data PasteleroR = UnPasteleroR { | |
nombrePastelero :: String, | |
ingredientesPastelero :: [Ingrediente] | |
} | |
deriving Show | |
marcosRecord = UnPasteleroR {nombrePastelero = "marcos", ingredientesPastelero = []} | |
-- modificando records: | |
-- para cambiar algun campo del record podemos usar la siguiente sintaxis | |
marcosPastelero = marcosRecord {nombrePastelero = "pastelero marcos"} | |
usarTodosLosIngRecord (UnPasteleroR nombre ingredientes) = UnPasteleroR nombre [] | |
data TuplaDe3 = UnaTuplaDe3 {fst3 :: Bool, snd3 :: Bool, thrd3 :: Bool} | |
deriving Show | |
tupla3 = UnaTuplaDe3 False True True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment