Skip to content

Instantly share code, notes, and snippets.

@kmels
Created August 22, 2012 14:52
Show Gist options
  • Save kmels/3426383 to your computer and use it in GitHub Desktop.
Save kmels/3426383 to your computer and use it in GitHub Desktop.
-- tests if an image is valid and, if it is, returns the resolution of the picture as a pair of the x-resolution and the y-resolution. An image is valid if all its pixels are valid and if all the rows have equally many columns.
ANTES
-----------------------------
validImage rows@(firstRow:nextRow:rrs) =
if (isValidRow firstRow && length firstRow == length nextRow && (isJust $ validImage (nextRow:rrs))) then
Just (length firstRow, length rows)
else
Nothing
validImage rows@(r:rs) = if isValidRow r && (isJust $ validImage rs) then Just (length r, length rows) else Nothing
DESPUES
-----------------------------
validImage :: [[RGB]] -> Maybe (Int, Int)
validImage [] = Just (0,0)
validImage rows = if all isValidRow rows && allEquals (map length rows)
then Just (length $ head rows,length rows)
else Nothing
allEquals (x:y:xs) = x == y && allEquals (y:xs)
allEquals [x] = True
allEquals [] = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment