Guest User

Untitled

a guest
Aug 4th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Practica 5
  2.  
  3. -- head
  4. hd :: [Int] -> Int
  5. hd (x:xs) = x
  6. -- tail
  7. tl :: [Int] -> [Int]
  8. tl (x:xs) = xs
  9. -- last
  10. -- FALTARIA VER QUE HACES EN EL CASO DE QUE HACES EL lt DE UNA LISTA VACIA
  11. lt :: [Int] -> Int
  12. -- lt [] = error "lista vacia"
  13. lt (x:xs) | (xs == []) = x
  14.           | otherwise = lt xs
  15. -- init
  16. -- FALTARIA VER QUE HACES EN EL CASO DE QUE HACES EL innit DE UNA LISTA VACIA
  17. innit :: [Int] -> [Int]
  18. -- innit [] = error "lista vacia"
  19. innit (x:xs) | (xs == []) = []
  20.              | otherwise = [x] ++ innit xs
  21.  
  22. -- Cuenta cuantos elementos hay en una lista
  23. -- FALTA EL PERFIL DE LA FUNCION
  24. -- FALTARIA VER QUE HACES EN EL CASO DE QUE HACES EL contatoria DE UNA LISTA VACIA
  25. -- contatoria [] = 0   
  26. contatoria (x:xs) | (xs == []) = 1
  27.                   | otherwise = 1 + contatoria xs
  28.  
  29. -- Devuelve True si el numero es primo..  False si no lo es
  30. esPrimo :: Int -> Bool
  31. esPrimo x | contatoria(divList x) == 2 = True
  32.           | otherwise = False
  33.                 where divList x = [n | n <- [1..x], (mod x n == 0)]
  34.  
  35. -- Tira una lista de numeros primos entre la lista de numeros pasadas
  36. -- FALTARIA VER QUE HACES EN EL CASO DE QUE HACES EL esPrimoLista DE UNA LISTA VACIA
  37. -- esPrimoLista [] = []
  38. esPrimoLista :: [Int] -> [Int]
  39. esPrimoLista (x:xs) | (xs == []) && (esPrimo x) = [x]
  40.                     | (xs == []) && (esPrimo x == False) = []
  41.                     | x == 1 = [x]++ esPrimoLista xs
  42.                     | esPrimo x = [x]++ esPrimoLista xs
  43.                     | otherwise = [] ++ esPrimoLista xs
  44.  
  45. -- Devuelve los numeros primos hasta el Numero dado
  46. esPrimoListNum :: Int -> [Int]
  47. esPrimoListNum x = esPrimoLista [1..(x-1)]
  48.  
  49. -- Revierte una lista upsideDown
  50. reversa :: [Int] -> [Int]
  51. reversa [] = []
  52. reversa (x:xs) = reversa xs ++ [x]
  53.  
  54. -- Devuelve True si las listas son Identicas
  55. compararListas :: [Int] -> [Int] -> Bool
  56. compararListas [] [] = True
  57. compararListas _ [] = False
  58. compararListas [] _ = False
  59. compararListas (x:xs) (y:ys) | (x == y) = compararListas xs ys
  60.                              | otherwise = False
  61.  
  62. -- Devuelve True si el numero es capicua
  63. palindromo :: [Int] -> Bool
  64. palindromo [] = True
  65. palindromo [x] = True
  66. palindromo (x:xs) | (x == lt xs) = palindromo (innit xs)
  67.                   | otherwise = False
  68.  
  69.  
  70.  
  71.  
  72. -- Une 2 listas previamente ordenadas de modo ordenado
  73. mergeLista :: [Int] -> [Int] ->[Int]
  74. mergeLista [] [] = []
  75. mergeLista x [] = x
  76. mergeLista [] y = y
  77. mergeLista (x:xs) (y:ys) | x <= y = [x] ++ mergeLista xs (y:ys)
  78.                          | otherwise = [y] ++ mergeLista (x:xs) ys
  79.  
  80. -- Ordena una lista
  81. quickSort :: [Int] -> [Int]
  82. quickSort [] = []
  83. quickSort (x:xs) = smallSort ++ [x] ++ bigSort
  84.                       where
  85.                           smallSort = quickSort [a | a <- (xs), a<= x];
  86.                           bigSort = quickSort [b | b <- (xs), b > x]
  87.  
  88. -- 2^n solo usando multiplicacion
  89. potDos :: Int -> Int
  90. potDos 0 = 1
  91. potDos x = 2 * potDos (x-1)
  92.  
  93. -- Devuelve un num binario de un decimal (Invertido)
  94. decABinarioInver :: Int -> [Int]
  95. decABinarioInver 0 = [0]
  96. decABinarioInver x | x < 2 = [1]
  97.                    | otherwise = [(mod x 2)] ++ decABinarioInver (div x 2)
  98.            --OTRA FORMA DE SOLUCIONAR EL INVERTIDO ES decABinarioInver (div x 2) ++ [(mod x 2)]    
  99. -- Arregla la funcion anterior
  100. decABinario :: Int -> [Int]
  101. decABinario x = reversa (decABinarioInver x)
  102.  
  103.  
  104. -- Cuadrado Perfecto
  105. cuadPerfecto :: Int -> Bool
  106. cuadPerfecto x  | [a | a <- [1..x], (a*a) == x] == [] = False
  107.                 | otherwise = True
  108.  
  109. -- Quitar una sublista de una lista
  110. -- FALTARIA OTRO CASO
  111. quitarDeLista :: [Int] -> [Int] -> [Int]
  112. -- quitarDeLista [] xs = xs
  113. quitarDeLista (x:xs) [] = []
  114. quitarDeLista (x:xs) (y:ys) | x == y = ys
  115.                             | otherwise = [y] ++ quitarDeLista (x:xs) ys
  116.             -- FIJATE QUE VOS LE ESTAS SACANDO EL PRIMER ELEMENTO DE LA SUBLISTA  A LA LISTA Y POR LO QUE ME DECIS
  117.             -- EN LA DESCRIPCION DE LA FUNCION TENES QUE SACAR TODA LA SUBLISTA DE LA LISTA
  118.             -- EJ: quitarDeLista [1,2] [1,2,3,4] => [3,4] Y VOS RETORNAS [2,3,4]       
  119.  
  120.  
  121. -- Devuelve True si una lista es permutacion de otra
  122. esPerm :: [Int] -> [Int] -> Bool
  123. esPerm [] [] = True
  124. esPerm _ [] = False
  125. esPerm [] _ = False
  126. esPerm (x:xs) ys | [a | a <- ys, a == x] /= [] = esPerm xs (quitarDeLista [a | a <- ys, a == x] ys)
  127.                  | otherwise = False
  128.  
  129.  
  130.  
  131. -- Suma los elementos de una lista
  132. sumaLista :: [Float] -> Float
  133. sumaLista [] = 0
  134. sumaLista (x:xs) = x + sumaLista xs
  135.  
  136.  
  137.  
  138. -- Devuelve el promedio de una lista dew Reales
  139. promReales :: [Float] -> Float
  140. promReales [] = 0
  141. promReales (x:xs) = (sumaLista (x:xs))/contatoria (x:xs)
  142.  
  143.  
  144. -- ===========================================
  145. -- Dada una lista, tenes que devolver una lista con todas las permutaciones posibles
  146. -- PERFIL DE LA FUNCION:
  147. -- permuta :: [Int] -> [[Int]]
  148. -- EJEM:  [1,2,3] => [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Add Comment
Please, Sign In to add comment