Skip to content

Instantly share code, notes, and snippets.

@harpiechoise
Created June 9, 2017 20:05
Show Gist options
  • Save harpiechoise/ecf90bf691ff8d07931aab868f169f2a to your computer and use it in GitHub Desktop.
Save harpiechoise/ecf90bf691ff8d07931aab868f169f2a to your computer and use it in GitHub Desktop.
promedio :: [Double] -> Double
promedio [] = 0
promedio bs= sum bs / fromIntegral(length bs)
factorial:: Integer -> Integer
factorial 0 = 1
factorial n = product[1..n]
--comb
comb n k = (factorial(n)) `div` ((factorial(k)) * factorial(n-k))
--impar
impar:: Integer -> Bool
impar n = (if mod n 2 == 0 then False else True)
--cuadrado
cuadrado:: Integer -> Integer
cuadrado 0 = 0
cuadrado n = n * n
--Suma de cuadrados
sumaDeCuadrados:: [Integer] -> Integer
sumaDeCuadrados [] = 0
sumaDeCuadrados xs = sum[cuadrado x | x <- xs]
--Suma de cuadrados
sumaDeCuadrados2:: [Integer] -> Integer
sumaDeCuadrados2 [] = 0
sumaDeCuadrados2 xs = sum[x*x | x <- xs]
--Ecuacion de segundo grado
raices_1 :: Double -> Double -> Double -> [Double]
raices_1 a b c = [ (-b+sqrt(b*b-4*a*c))/(2*a),
(-b-sqrt(b*b-4*a*c))/(2*a) ]
--signum
n_signum:: Integer -> Integer
n_signum x | x > 0 = 1
| x == 0 = 1
| otherwise = 0
--Anterior
anterior:: Integer -> Integer
anterior x | x /= 0 = x - 1
| x < 0 = x + 1
| otherwise = 0
--Potencia
potencia:: Integer -> Integer -> Integer
potencia x 0 = 1
potencia x y = x^y
--Siguiente
siguiente:: Int -> Int
siguiente x | x > 0 = x + 1
| otherwise = 0
--Doble
doble:: Integer -> Integer
doble 0 = 0
doble x = 2 * x
--Mitad
mitad:: Float -> Float
mitad 0 = 0
mitad x = x / 2
--cc a litros
convCC:: Float -> Float
convCC 0 = 0
convCC cc = (cc*(1) / 1000)
--cc segundos a litros por hora
convCC's:: Float -> Float
convCC's 0 = 0
convCC's cc = (cc * 3600) / 1000
--lucky
lucky:: (Integral a) => a -> String
lucky 7 = "Estas de suerte"
lucky 0 = "No es tu dia hoy :C"
--Say me
sayMe:: (Integral a) => a -> String
sayMe 0 = "Cero"
sayMe 1 = "Uno"
sayMe 2 = "Dos"
sayMe 3 = "Tres"
sayMe 4 = "Cuatro"
sayMe 5 = "Cinco"
sayMe x = "Es mayor a cinco :C"
--Fact Rec
fact:: (Integral a) => a -> a
fact 0 = 1
fact x = x * fact (x - 1)
--add vectors
addVec:: (Num a) => (a, a) -> (a, a) -> (a, a)
addVec (x1, x2) (b1, b2) = (x1 + b1, x2 + b2)
--Functions in triplas
first:: (a,b,c) -> a
first (a,_,_) = a
second:: (a,b,c) -> b
second (_,b,_) = b
third:: (a,b,c) -> c
third (_,_,c) = c
--Head
hed:: [a] -> a
hed xs = xs !! 0
bmTell:: (RealFloat a) => a -> String
bmTell x | x <= 18.5 = "Estas Bajo Peso"
| x <= 40.5 = "Eres normal :c"
| x <= 80.0 = "Eres Gordross"
| otherwise = "Eres una ballena"
--peso ideal
imc:: Float -> Float -> Float
imc peso altura = peso / (altura ^2)
eGor:: Float -> Float -> String
eGor peso altura | imc peso altura < 18.5 = "Estas bajo peso"
| imc peso altura <= 24.9 = "Estas normal"
| imc peso altura <= 29.9 = "Estas Con Sobre Peso "
| imc peso altura <= 34.9 = "Tienes obesidad leve"
| imc peso altura <= 39.9 = "Tienes Obesidad Morbida"
| imc peso altura >= 40.0 = "Tienes un problema"
| otherwise = "Tienes un problema serio"
pesId:: Float -> Float -> [Float]
pesId peso altura = [ x | x <- [1..peso], imc x altura < 24.9 && imc x altura >= 18.5]
--nombre y apellido
nombres = ["Jaime","Sofia","Guillermo","Macarena","Lola","Alicia","Fernando"]
apellidos = ["Fernandez", "Baeza", "Alvares","Tapia","Crispi","Lira"]
mezcla:: [String] -> [String] -> [String]
mezcla nombres apellidos = [x ++" "++y| x <- nombres, y <- apellidos ]
--numeros perfectos
factores:: Integer -> [Integer]
factores n= [x | x <- [1..n-1], mod n x == 0]
numA:: Integer -> [Integer]
numA n = [x | x <- [1..n], sum(init(factores x)) == x]
--numeros abundantes
numAb:: Integer -> Bool
numAb n | n < sum(factores n) = True
| otherwise = False
--numeros Abundantes menores
numAbm:: Integer -> [Integer]
numAbm n = [x | x <- [1..n], numAb x == True]
--numeros abundantes pares
numAbmp:: Integer -> [Integer]
numAbmp n = [x | x <- [1..n],numAb x == True && mod x 2 == 0]
--numeros abundantes impares
numAbmi = head[x | x <- [1..], numAb x , odd x ]
--Describir una lista
describeList:: [a] -> String
describeList xs = "Esta Es " ++ case xs of [] -> "Una Lista Vacia"
[x] -> "Una Lista unitaria"
xs -> "Una Lista Larga"
describe':: [a] -> String
describe' xs = "Esta Es" ++ lista xs
where lista [] = " Una lista Vacia"
lista [x] = " Una lista Unitaria"
lista xs = " Una lista larga"
--peso 2da manera
eGor':: Float -> Float -> String
eGor' peso altura | imc peso altura < muyFlaco = "Estas bajo peso"
| imc peso altura <= normal = "Estas normal"
| imc peso altura <= sobrePeso = "Estas Con Sobre Peso "
| imc peso altura <= ob1 = "Tienes obesidad leve"
| imc peso altura <= ob2 = "Tienes Obesidad Morbida"
| imc peso altura >= prob = "Tienes un problema"
| otherwise = "Tienes un problema serio"
where muyFlaco = 18.5
normal = 24.9
sobrePeso = 29.9
ob1 = 34.9
ob2 = 39.9
prob = 40.0
--Ecuacion de segundo grado
ec2grad:: Float -> Float -> Float -> [Float]
ec2grad a b c | (b^2) - ((4*a)*c)< 0 = [0,0]
| otherwise = [rpos a b c, rneg a b c]
where rpos a b c = (((-1)*(b)) + (sqrt ((b^2) - ((4*a)*c)))) / (2*a)
rneg a b c = (((-1)*(b)) - (sqrt ((b^2) - ((4*a)*c)))) / (2*a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment