Skip to content

Instantly share code, notes, and snippets.

@carlisgg
Created November 28, 2011 21:14
Show Gist options
  • Save carlisgg/1402096 to your computer and use it in GitHub Desktop.
Save carlisgg/1402096 to your computer and use it in GitHub Desktop.
7L7W - Haskell
module Main where
-- Primer ejercicio
vuelta [] = []
vuelta (h:t) = vuelta t ++ [h]
-- Segundo ejercicio
combinations aList = [(a, b) | a <- aList, b <- aList, a < b]
-- Tercer ejercicio
multitable = [ (a, b, a * b) | a <- [1..12], b <- [1..12]]
-- Cuarto ejercicio
colors = ["red", "green", "blue"]
coloring = [(alabama, mississippi, georgia, tennessee, florida) | alabama <- colors, mississippi <- colors, georgia <- colors,
tennessee <- colors, florida <- colors, alabama /= mississippi, tennessee /= mississippi, alabama /= tennessee, alabama /= mississippi,
alabama /= georgia, alabama /= florida, georgia /= tennessee, georgia /= florida]
module Main where
-- Primer ejercicio
ordena [] = []
ordena (h:t) = ordena (filter ( <= h) t) ++ [h] ++ ordena (filter ( > h) t)
-- Segundo ejercicio
ordenacustom [] comp = []
ordenacustom (h:t) comp = ordenacustom (filter ((/= h) . head . comp h) t) comp ++ [h] ++ ordenacustom (filter ((== h) . head . comp h) t) comp
-- Probar con ordenacustom [1,3,5,2,3,6] (\x y -> if (x < y) then [x, y] else [y, x])
-- Tercer ejercicio
multrange start multiplier = start:(multrange (start * multiplier) multiplier)
digit2int char = fromEnum char - (fromEnum '0')
isDigit char = char >= '0' && char <= '9'
conv2number (str) =
fromIntegral (foldl (+) 0
(zipWith (*)
(take (length str) (multrange 1 10))
(map (digit2int) (reverse (filter (isDigit) str)))
)
) / 100.0
-- Cuarto ejercicio
everythird (x)
| length x > 2 = (last (take 3 x)):(everythird (drop 3 x))
| otherwise = []
everyfifth (x)
| length x > 5 = (last (take 3 x)):(everythird (drop 3 x))
| otherwise = []
-- Quinto ejercicio
half = (/ 2)
append str1 str2 = str1 ++ str2
toline = (flip append) "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment