Algunos ejercicios hechos en Haskell.
Haskell
Haskell es un lenguaje de programación estandarizado puramente funcional con semántica no estricta, llamado así por el lógico Haskell Curry. Es uno de los lenguajes funcionales más populares y el lenguaje funcional perezoso en el que se está realizando la mayor parte de la investigación.
Extensión: *.hs y *.lhs
Compilar
$ ghc holamundo.hs
Ejecutar
$ ./holamundo
Cargar script
$ ghci
> 12 + 45
> [1,2,3,4,5]
> let x = [3,9,6]
> :cd RUTA_DEL_SCRIPT
> :l script.hs
> :! clear
> :q
Operaciones básicas
$ ghci
> rem 120 100
> 12 + 54
> "Hola, mundo mundial del mundo universal"
> not True
> not False
> True || True
> False || True
> True && True
> False && True
> not (True || True)
> not (9/=3)
> 8/=6
> let nombre ="Juan Ortiz"
> nombre
> "Juan Pablo"/=nombre
> "Dan "++"Kerr"
> nombre++" Alvarez"
> let lista = [1,2,3]
> length lista
> head lista
> tail lista
> let cierto = if 7>3 then "X" else "Y"
> cierto
> :! clear
> :! pwd
> :! ls
> :q
Comentarios
--Este es un simple comentario en Haskell
Comentarios multilínea
{-
Este es
un comentario
multi línea en
Haskell
-}
sumar.hs
add :: Integer -> Integer -> Integer
add x y = x+y
{-
$ ghci
> :cd RUTA_DEL_SCRIPT
> :l sumar.hs
> sumar 4 5 --9
> sumar 6 2 --8
-}
Síntaxis básica para crear una función
-- definimos función
nombreFunc :: Tipo -> Tipo ->Tipo
nombreFunc x 0 = x
nombreFunc 0 y = y
nombreFunc x y = x - y
Estructuras selectivas
if 2 > 0
then "Verdadero: 2 es mayor a cero"
else "Falso: 2 es menor a cero"
let decide = if 2 > 0 then "Verdadero: 2 es mayor a cero" else "Falso: 2 es menor a cero"
Sitio oficial:
- https://www.haskell.org/
- http://gettingsharper.de/tag/haskell/
- http://learnyouahaskell.com/
- http://rosettacode.org/wiki/Category:Haskell
- https://hoogle.haskell.org/?hoogle=getLine
- https://wiki.haskell.org/Tutorials/Programming_Haskell/Argument_handling
- http://www.willamette.edu/~fruehr/haskell/evolution.html
- http://book.realworldhaskell.org/
- Haskell por el bien de todos
- Learning Haskell
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 System.Environment | |
main = do | |
args <- getArgs | |
putStrLn "Los argumentos son:" | |
mapM_ putStrLn args |
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 System.Environment | |
main = do | |
args <- getArgs | |
file <- if null args | |
then do putStr "Archivo: " ; getLine | |
else pure (head args) | |
putStr "Procesando..." | |
putStrLn file | |
writeFile file "Hoy es 25 de abril de 2021" | |
{- | |
$ ghc argumentos_print.hs | |
$ ./argumentos_print archivo.dat | |
-} |
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
main = do | |
let sueldo=9000 | |
let edo_civil="soltero" | |
putStrLn("Sueldo inicial: "++show sueldo) | |
--funciones | |
--let aumento sueldo = (0.25*sueldo) + sueldo | |
--let bono sueldo = sueldo + 900 | |
--let total b s = b + s | |
--putStrLn("Aumento: "++show(aumento(sueldo))) | |
--putStrLn("Bono: "++show(bono(sueldo))) | |
if sueldo < 10000 | |
then | |
do | |
let aumento sueldo = (0.25*sueldo) + sueldo | |
--let bono sueldo = sueldo + 900 | |
--putStrLn("Bono: "++show(bono(sueldo))) | |
putStrLn("Aumento: "++show(aumento(sueldo))) | |
--let total bono aumento = bono + aumento | |
--putStrLn("Sueldo final: "++show(total(bono,aumento))) | |
else | |
putStrLn "El sueldo es mayor o igual a 10000. No recibe aumento." | |
if edo_civil == "casado" | |
then | |
do | |
let bono sueldo = sueldo + 900 | |
putStrLn("Bono: "++show(bono(sueldo))) | |
else | |
putStrLn "El empleado es casado. No recibe bono." |
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
{- | |
No. combinatorio | |
-} | |
fact :: Int -> Int | |
fact 0 = 1 | |
fact n = fact(n-1)*n | |
combinatorio :: Int -> Int -> Int | |
combinatorio x 0 = 0 | |
combinatorio 0 y = 0 | |
combinatorio x y = fact(x) `div` fact(x)*fact(x-y) | |
-- Uso: | |
{- | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l combinatorio.hs | |
> combinatorio 10 3 | |
-} |
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
{- | |
Esto es un comentario multilinea en Haskell | |
[1,2,3] == [1,2,3] = True | |
[1,2] == [1,2,3] = False | |
[] == [1,2] = False | |
[1,2,3] == [] = False | |
-} | |
{- | |
*Main> comp_listas [1] [] | |
False | |
*Main> comp_listas [1] [1] | |
True | |
*Main> comp_listas [1,2] [2,1] | |
False | |
*Main> comp_listas [] [2,1] | |
False | |
*Main> comp_listas [] [1] | |
False | |
*Main> comp_listas [32] [30+2] | |
True | |
-} | |
comp_listas :: [Int] -> [Int] -> Bool | |
comp_listas [] [] = True | |
comp_listas [] _ = False | |
comp_listas _ [] = False | |
comp_listas (a:b) (c:d) | (a == c) = comp_listas b d | |
| otherwise = False |
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
cubo :: Int -> Int | |
cubo num = num * num * num |
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
cuentaCifras lista = [if num<10 then "Una cifra" else "Dos cifras" | |
| num <- lista | |
, odd num] |
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
decide :: Int -> String | |
decide x = if x > 0 | |
then "Verdadero: "++ show x ++" es mayor a cero" | |
else "Falso: "++show x++" es igual a cero" |
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
divisible x y = if (x `mod` y) == 0 | |
then "son divisibles" | |
else "no son divisibles" |
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
doubleSmall x = if x > 100 | |
then x | |
else x*2 |
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
$ ghci | |
> :? | |
> :! pwd | |
> :cd /home/user/Documentos/scripts | |
> :! cat funcion.hs | |
> :l programa.hs | |
> programa variable | |
> :! clear | |
> :! ls | |
> :edit programa.hs | |
> :list |
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
esMayor x y = if x > y | |
then show x ++" es mayor" | |
else show y ++" es mayor" |
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
euler :: Double -> Double | |
euler 0.0 = 1.0 | |
euler n = 1.0 / product [1..n] + euler (n - 1.0) |
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
factorial 0 = 1 | |
factorial n = factorial(n-1)*n |
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
fibonacci 0 = 0 | |
fibonacci 1 = 1 | |
fibonacci n = fibonacci(n-1) + fibonacci(n-2) |
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
func :: (Int, Int) -> (Int, Int) -> (Int, Int) | |
func (a, b) (c, d) = (a+c, b+d) |
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
sumaCinco :: Int -> Int | |
sumaCinco x = x+5 | |
multiplicaPorTres :: Int -> Int | |
multiplicaPorTres x = x*3 | |
funcionDeAltoOrden f g x = f(g(x)) | |
{- | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l funciones.hs | |
> funcionDeAltoOrden multiplicaPorTres sumaCinco 6 | |
33 | |
-} |
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
{- | |
Fundamentos en Haskell | |
1. Entrar al shell (repl) | |
$ ghci | |
> :cd /home/fernando/Documentos/pruebasHaskell | |
> :l | |
> :q -- Salir del shell | |
-} | |
{- | |
5. | |
-} | |
{- | |
4. Creando tipos | |
-} | |
type Entero = Int | |
triple :: Entero -> Entero | |
triple 0 = 0 | |
triple n = n * 3 | |
{- | |
nombreFunc :: Tipo -> Tipo -> Tipo | |
nombreFunc a b = a + b | |
En lenguajes como Javascript sería: | |
function nombreFunc(a, b){ | |
return a + b; | |
} | |
o | |
var nombreFunc = (a, b) => a+b; | |
-} | |
{- | |
RESULT=0 | |
X=0 | |
Y=1 | |
IF X==0 THEN | |
RESULT = Y | |
END IF | |
IF Y==0 THEN | |
RESULT = X | |
END IF | |
IF X!=0 AND Y!=0 THEN | |
RESULT = X+Y | |
END IF | |
3. Crear funcion con dos parametros(x,y). Si x es 0, el resultado es y. Si y es 0, el resultado es x. Si ambos son distintos de 0, e resultado es la suma de x+y. | |
-} | |
nombreFunc :: Int -> Int -> Int | |
nombreFunc x 0 = x | |
nombreFunc 0 y = y | |
nombreFunc x y = x+y | |
{- | |
2. Función que devuelva 1 si el valor de entrada es 0 | |
Ej. | |
factorial 0 = 1 | |
factorial n = factorial(n-1)*n | |
En Javascript sería: | |
function factorial(num){ | |
if(num <= 0){ | |
return 1; | |
}else{ | |
return factorial(num-1)*num; | |
} | |
} | |
let valor = factorial(0); // 1 | |
valor = factorial(5);// 120 | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l fundamentos.hs | |
> verifica 0 -- 1 | |
> verifica 2 -- 4 | |
-} | |
verifica :: Int -> Int | |
verifica 0 = 1 | |
verifica n = n*2 | |
{- | |
-- 1. funciones | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l fundamentos.hs | |
> sumaFunc 4 3 | |
> restaFunc 4 5 | |
> suma = sumaFunc 5 4 | |
> suma | |
> resta = restaFunc 10 5 | |
> resta | |
> operaFunc suma resta | |
-} | |
sumaFunc :: Int -> Int -> Int | |
sumaFunc x y = x+y | |
restaFunc :: Int -> Int -> Int | |
restaFunc x y = x-y | |
operaFunc :: Int -> Int -> Int | |
operaFunc x y = x*y |
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
guarda x | (x == 0) = 0 | |
| (x == 1) = 1 | |
| otherwise = 10 |
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
hola :: IO() | |
hola = putStrLn "Hola, mundo en Haskell!!" |
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
module Main where | |
main :: IO() | |
main = do | |
putStrLn "Hola, mundo en Haskell!!" | |
-- $ ghc holamonde.hs | |
-- $ ./holamonde |
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
--Compilar: ghc holamundo.hs | |
--Ejecutar: ./holamundo | |
main = putStrLn "Bienvenido al mundo Haskell!" |
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
siEsMayor m n = if m > n | |
then show m ++ " es mayor" | |
else show n ++ " es mayor" | |
{- | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l if.hs | |
> siEsMayor 12 3 | |
> siEsMayor 3 44 | |
> :q | |
-} | |
{- | |
if 2 > 0 | |
then "Verdadero: 2 es mayor a cero" | |
else "Falso: 2 es menor a cero" | |
-} |
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
--Obtener la longitud de una lista | |
longitud lista = sum[ 1 | x <-lista] | |
{- | |
$ ghci | |
> :cd RUTA_DEL_SCRIPT | |
> :l longitud.hs | |
> let lista = [1..30] | |
> longitud lista | |
> :q | |
-} |
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
mcd::Int->Int->Int | |
mcd x 0 = x | |
mcd x y = mcd y (mod x y) | |
{- | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l maximocomundiv.hs | |
> mcd 3 4 | |
> mcd 5 6 | |
> :q | |
-} |
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
my_and :: Bool -> Bool -> Bool | |
my_and False _ = False | |
my_and _ False = False | |
my_and True True = True |
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
nombres :: (String, String, String) | |
nombres = ("Fernando","Uriel", "Alma") | |
select_prim (x, _, _) = x | |
select_seg (_, y, _) = y | |
select_ter (_, _, z) = z |
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
type Nombre = String | |
type Edad = Int | |
type Idioma = String | |
type Persona = (Nombre, Edad, Idioma) | |
persona::Persona | |
persona = ("Horacio", 1994, "Portugues") | |
primero :: Persona -> Nombre | |
primero (n, e, i) = n | |
segundo :: Persona -> Edad | |
segundo (n, e, i) = e | |
tercero :: Persona -> Idioma | |
tercero (n, e, i) = i |
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
primes = filterPrime [2..] | |
where filterPrime (p:xs) = | |
p : filterPrime [x | x <- xs, x `mod` p /= 0] | |
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
promedio :: Float -> Float -> Float | |
promedio a b = (a+b)/2.0 | |
{- | |
$ ghci | |
> :cd RUTA_SCRIPT | |
> :l promedio.hs | |
> promedio 9 4 | |
> 33.08 `promedio` 11.5 `promedio` 12.1 | |
-} |
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
restar :: Int -> Int -> Int | |
restar 0 y = y | |
restar x 0 = x | |
restar x y = x - y | |
{- | |
$ ghci | |
> :cd /home/user/Documentos/pruebasHaskell | |
> :l restar.hs | |
> restar 3 2 | |
> restar 10 5 | |
-} |
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
restoDeDosNum m n = m `mod` n |
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
--author: Fer Carraro | |
main = do | |
putStrLn "Introduce tu nombre:" | |
nombre <- getLine | |
putStrLn("Hola, "++nombre) | |
--Esto es un comentario |
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
suma 1 = 1 | |
suma n = suma(n-1) + n |
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
suma :: Int -> Int -> Int | |
suma x y = x + y | |
{- | |
$ ghci | |
> :cd RUTA_SCRIPTS | |
> :l suma1.hs | |
> suma 3 4 | |
> suma 8 3 | |
-} |
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
sumaDiez x = x + 10 |
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
sumando :: Integer -> Integer -> Integer | |
sumando x y = x+y | |
{- | |
$ ghci | |
> :cd RUTA_DEL_SCRIPT | |
> :l sumando.hs | |
> sumando | |
-} |
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
sumaNumeros x y = x + y |
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
sumaTres num = num + num + num | |
{- | |
$ ghci | |
> :cd RUTA_DEL_SCRIPT | |
> :l sumaTres | |
> sumaTres 9 --27 | |
> sumaTres 88 --264 | |
> :q | |
-} |
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
--listas | |
size_list [] = 0 | |
size_list (x:xs) = 1 + size_list xs |
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
verdadero :: Bool -> Bool -> Bool | |
verdadero x y = x && y |
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
vocales frase = [ letra | letra <- frase, | |
letra `elem` ['a','e','i','o','u'] ] | |
{- | |
$ ghci | |
> :cd RUTA_DEL_SCRIPT | |
> :l vocales.hs | |
> let frase = "europa renacida" | |
> vocales frase | |
> :q | |
-} |
Comentarios
Publicar un comentario