Datos compuestos de Haskell como modelo de datos
From Ibbddunq
(Difference between revisions)
Line 3: | Line 3: | ||
data CursadaAlumno = | data CursadaAlumno = | ||
- | Cursada Legajo Materia Cuatrimestre Nota | | + | Cursada Legajo Materia Cuatrimestre Nota |
- | + | deriving (Eq, Show) | |
+ | |||
+ | data ExamenIntegradorAlumno = | ||
+ | ExamenIntegrador Legajo Materia Cuatrimestre Nota | ||
+ | deriving (Eq, Show) | ||
+ | |||
+ | data Alumno = | ||
+ | Alumno Legajo Nombre Fecha | ||
+ | deriving (Eq, Show) | ||
+ | |||
+ | type Materia = String | ||
+ | |||
+ | type Cuatrimestre = (Int,Int) | ||
+ | |||
+ | type Nota = Int | ||
+ | |||
+ | type Legajo = String | ||
+ | |||
+ | type Nombre = String | ||
+ | |||
+ | type Fecha = (Int, Int, Int) | ||
+ | |||
+ | |||
+ | registroCursadas = [ | ||
+ | Cursada "128/1" "Orga1" (2008,01) 9, | ||
+ | Cursada "131/4" "Orga1" (2008,01) 5, | ||
+ | Cursada "143/2" "IPr" (2008,01) 6, | ||
+ | Cursada "201/9" "IPr" (2008,01) 8, | ||
+ | Cursada "322/8" "IPr" (2008,01) 2, | ||
+ | Cursada "128/1" "IPr" (2008,01) 6 ] | ||
+ | |||
+ | registroExamenesIntegradores = [ | ||
+ | ExamenIntegrador "143/2" "IPr" (2008,01) 5, | ||
+ | ExamenIntegrador "128/1" "IPr" (2008,01) 2 ] | ||
+ | |||
+ | |||
+ | alumnos = | ||
+ | [ Alumno "143/2" "Juan Lopez" (23,2,1988), | ||
+ | Alumno "201/9" "Roque Granata" (15,6,1981), | ||
+ | Alumno "322/8" "Agustin Bal" (2,9,1984), | ||
+ | Alumno "128/1" "Jose de Zer" (18,11,1977), | ||
+ | Alumno "131/4" "Lucas E. Rito" (21,12,1987) ] | ||
+ | |||
+ | |||
+ | nombre legajo = nombre' legajo alumnos | ||
+ | |||
+ | nombre' legajo (Alumno leg nom _:resto) | ||
+ | | leg == legajo = nom | ||
+ | | otherwise = nombre' legajo resto | ||
+ | |||
+ | aprobo alumno materia = | ||
+ | aproboCursada alumno materia || | ||
+ | aproboExamenIntegrador alumno materia | ||
+ | |||
+ | aproboCursada alumno materia = aproboCursada' alumno materia registroCursadas | ||
+ | |||
+ | aproboCursada' alumno materia [] = False | ||
+ | aproboCursada' alumno materia (Cursada al mat _ nota:resto) | ||
+ | | al == alumno && mat == materia && nota >= 7 = True | ||
+ | | otherwise = aproboCursada' alumno materia resto | ||
+ | |||
+ | |||
+ | aproboExamenIntegrador alumno materia = | ||
+ | aproboExamenIntegrador' alumno materia registroExamenesIntegradores | ||
+ | |||
+ | aproboExamenIntegrador' alumno materia [] = False | ||
+ | aproboExamenIntegrador' alumno materia (ExamenIntegrador al mat _ nota:resto) | ||
+ | | al == alumno && mat == materia && nota >= 4 = True | ||
+ | | otherwise = aproboExamenIntegrador' alumno materia resto | ||
+ | |||
+ | |||
+ | planillaCurso mat cuat = planillaCurso' mat cuat registroCursadas | ||
+ | |||
+ | planillaCurso' mat cuat [] = [] | ||
+ | |||
+ | planillaCurso' mat cuat (Cursada leg m c nota:resto) | ||
+ | | mat == m && cuat == c = (nombre leg, nota):planillaCurso' mat cuat resto | ||
+ | | otherwise = planillaCurso' mat cuat resto | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Lo que sigue es una versión hecha con herramientas alternativas a la recursividad | ||
+ | |||
+ | data CursadaAlumno = | ||
+ | Cursada Legajo Materia Cuatrimestre Nota | ||
deriving (Eq, Show) | deriving (Eq, Show) | ||
Line 56: | Line 141: | ||
legajoAlumno (Alumno leg _ _) = leg | legajoAlumno (Alumno leg _ _) = leg | ||
- | cursadas alumno materia | + | cursadas alumno materia = filter (esCursada alumno materia) registroCursadas |
- | + | ||
- | + | ||
- | + | ||
examenes alumno materia = | examenes alumno materia = | ||
Line 74: | Line 156: | ||
aproboCursada (Cursada _ _ _ nota) = nota >= 7 | aproboCursada (Cursada _ _ _ nota) = nota >= 7 | ||
- | |||
- | |||
aproboExamenIntegrador (ExamenIntegrador _ _ _ nota) = nota >= 4 | aproboExamenIntegrador (ExamenIntegrador _ _ _ nota) = nota >= 4 |
Current revision as of 22:22, 2 January 2008
Este programa incluye una DB sobre notas de alumnos, tomando como modelo el sistema de tipos de Haskell. También incluye algunas consultas sobre la BD
data CursadaAlumno = Cursada Legajo Materia Cuatrimestre Nota deriving (Eq, Show) data ExamenIntegradorAlumno = ExamenIntegrador Legajo Materia Cuatrimestre Nota deriving (Eq, Show) data Alumno = Alumno Legajo Nombre Fecha deriving (Eq, Show) type Materia = String type Cuatrimestre = (Int,Int) type Nota = Int type Legajo = String type Nombre = String type Fecha = (Int, Int, Int) registroCursadas = [ Cursada "128/1" "Orga1" (2008,01) 9, Cursada "131/4" "Orga1" (2008,01) 5, Cursada "143/2" "IPr" (2008,01) 6, Cursada "201/9" "IPr" (2008,01) 8, Cursada "322/8" "IPr" (2008,01) 2, Cursada "128/1" "IPr" (2008,01) 6 ] registroExamenesIntegradores = [ ExamenIntegrador "143/2" "IPr" (2008,01) 5, ExamenIntegrador "128/1" "IPr" (2008,01) 2 ] alumnos = [ Alumno "143/2" "Juan Lopez" (23,2,1988), Alumno "201/9" "Roque Granata" (15,6,1981), Alumno "322/8" "Agustin Bal" (2,9,1984), Alumno "128/1" "Jose de Zer" (18,11,1977), Alumno "131/4" "Lucas E. Rito" (21,12,1987) ] nombre legajo = nombre' legajo alumnos nombre' legajo (Alumno leg nom _:resto) | leg == legajo = nom | otherwise = nombre' legajo resto aprobo alumno materia = aproboCursada alumno materia || aproboExamenIntegrador alumno materia aproboCursada alumno materia = aproboCursada' alumno materia registroCursadas aproboCursada' alumno materia [] = False aproboCursada' alumno materia (Cursada al mat _ nota:resto) | al == alumno && mat == materia && nota >= 7 = True | otherwise = aproboCursada' alumno materia resto aproboExamenIntegrador alumno materia = aproboExamenIntegrador' alumno materia registroExamenesIntegradores aproboExamenIntegrador' alumno materia [] = False aproboExamenIntegrador' alumno materia (ExamenIntegrador al mat _ nota:resto) | al == alumno && mat == materia && nota >= 4 = True | otherwise = aproboExamenIntegrador' alumno materia resto planillaCurso mat cuat = planillaCurso' mat cuat registroCursadas planillaCurso' mat cuat [] = [] planillaCurso' mat cuat (Cursada leg m c nota:resto) | mat == m && cuat == c = (nombre leg, nota):planillaCurso' mat cuat resto | otherwise = planillaCurso' mat cuat resto
Lo que sigue es una versión hecha con herramientas alternativas a la recursividad
data CursadaAlumno = Cursada Legajo Materia Cuatrimestre Nota deriving (Eq, Show) data ExamenIntegradorAlumno = ExamenIntegrador Legajo Materia Cuatrimestre Nota deriving (Eq, Show) data Alumno = Alumno Legajo Nombre Fecha deriving (Eq, Show) type Materia = String type Cuatrimestre = (Int,Int) type Nota = Int type Legajo = String type Nombre = String type Fecha = (Int, Int, Int) registroCursadas = [ Cursada "128/1" "Orga1" (2008,01) 9, Cursada "131/4" "Orga1" (2008,01) 5, Cursada "143/2" "IPr" (2008,01) 6, Cursada "201/9" "IPr" (2008,01) 8, Cursada "322/8" "IPr" (2008,01) 2, Cursada "128/1" "IPr" (2008,01) 6 ] registroExamenesIntegradores = [ ExamenIntegrador "143/2" "IPr" (2008,01) 5, ExamenIntegrador "128/1" "IPr" (2008,01) 2 ] alumnos = [ Alumno "143/2" "Juan Lopez" (23,2,1988), Alumno "201/9" "Roque Granata" (15,6,1981), Alumno "322/8" "Agustin Bal" (2,9,1984), Alumno "128/1" "Jose de Zer" (18,11,1977), Alumno "131/4" "Lucas E. Rito" (21,12,1987) ] nombre legajo = (nombreAlumno . head . (filter ((legajo ==).legajoAlumno))) alumnos nombreAlumno (Alumno _ nombre _) = nombre legajoAlumno (Alumno leg _ _) = leg cursadas alumno materia = filter (esCursada alumno materia) registroCursadas examenes alumno materia = filter (esExamen alumno materia) registroExamenesIntegradores esCursada alumno materia (Cursada al mat _ _) = alumno == al && materia == mat esExamen alumno materia (ExamenIntegrador al mat _ _) = alumno == al && materia == mat aprobo alumno materia = any aproboCursada (cursadas alumno materia) || any aproboExamenIntegrador (examenes alumno materia) aproboCursada (Cursada _ _ _ nota) = nota >= 7 aproboExamenIntegrador (ExamenIntegrador _ _ _ nota) = nota >= 4 planillaCurso materia cuatrimestre = [ (nombre leg, nota) | (Cursada leg _ _ nota) <- curso materia cuatrimestre ] curso materia cuatrimestre = filter esCurso registroCursadas where esCurso (Cursada _ mat cuat _) = mat == materia && cuat == cuatrimestre