a) modified this a bit because its more complicated than it needed

to be, in two ways. first, the map and filter situation was uneccesary.
list comprehension with a verify predicate is more easily followed.
b) the monadic bind thing could be cool in the predicate, as i could
create one mega computation for the verify, but its entirely unecessary,
and makes each individual check more complex than it needs to be.
c) this is simple enough that type annotations are unecessary.
d) stopped trying to do io or using the main function. it seems sensible
to treat this as a library of functions rather than a single program to be
executed.
e) the checks here are somewhat specific, it makes sense to make them
only visible to the passcodes function.
This commit is contained in:
aliasless 2019-12-04 17:10:57 -08:00
parent b15b5ebdbe
commit 51132fd27c
4 changed files with 9 additions and 27 deletions

6
4.2/4.2.hs Normal file
View File

@ -0,0 +1,6 @@
import Data.List
import Data.List.Ordered
passcodes = length [ x | x <- [254042 .. 789860], ascending x, adjacents x]
where ascending = isSorted . show
adjacents num = any (\digit -> length digit == 2) (group $ show num)

View File

@ -1,15 +0,0 @@
import Data.List
import Data.List.Ordered
import Data.Maybe
ascending :: Int -> Maybe Int
ascending num = case isSorted $ show num of True -> Just num
False -> Nothing
adjacents :: Int -> Maybe Int
adjacents num = case any (\digit -> length digit == 2) (group $ show num) of True -> Just num
False -> Nothing
main = do
putStrLn $ show $ length $ filter (isJust) (map (verify) [254042 .. 789860])
where verify num = ascending num >>= adjacents

Binary file not shown.

15
4/4.hs
View File

@ -1,15 +1,6 @@
import Data.List
import Data.List.Ordered
import Data.Maybe
ascending :: Int -> Maybe Int
ascending num = case isSorted $ show num of True -> Just num
False -> Nothing
adjacents :: Int -> Maybe Int
adjacents num = case any (\digit -> length digit >= 2) (group $ show num) of True -> Just num
False -> Nothing
main = do
putStrLn $ show $ length $ filter (isJust) (map (verify) [254042 .. 789860])
where verify num = ascending num >>= adjacents
passcodes = length [ x | x <- [254042 .. 789860], ascending x, adjacents x]
where ascending = isSorted . show
adjacents num = any (\digit -> length digit >= 2) (group $ show num)