haskell - Getting positions from a list of lists -
rellevant types:
data sudoku = sudoku [[maybe int]] type block = [maybe int] rows :: sudoku -> [[maybe int]] rows (sudoku rs) = rs
i have list of lists, [[maybe int]], , i've stated
type block = [maybe int]
the assignment working on creating sudoku solver, i'd need function homecoming position of 'blank' cell, blank beingness nothing. actual layout of sudoku
sudoku [ , list1 , list2 .... ]
until have 9 lists, each containing 9 elements, nil or int.
type pos = (int, int)
the first int in pos should indicate 'row' contains blank cell. sec 1 should indicate element whithin row blank one. i've written
whichrow :: sudoku -> int whichrow (sudoku (x:xs)) = if isnothingpresent x == false 1 + whichrow (sudoku xs) else 1 whereisnothing :: block -> int whereisnothing (x:xs) = if x == nil 1 else 1 + whereisnothing xs isnothingpresent :: block -> bool isnothingpresent b | nil `notelem` b = false | otherwise = true
now need combine these functions , create function blank examine sudoku , homecoming position of blank cell. played around , tried:
blank :: sudoku -> pos blank sud = k <- (whichrow sud) n <- (whereisnothing (head (drop (k-1) (rows sud)))) (return (k, n))
which wrong, type errors , kinds of stuff. , looks wierd block there aswell. point me in right direction? thanks!
i these errors:
sudoku.hs:159:22: couldn't match expected type `(int, int)' actual type `int' in stmt of 'do' block: k <- (whichrow sud) in expression: { k <- (whichrow sud); n <- (whereisnothing (head (drop (k - 1) (rows sud)))); (return (k, n)) } sudoku.hs:160:22: couldn't match expected type `(int, t0)' actual type `int' in stmt of 'do' block: n <- (whereisnothing (head (drop (k - 1) (rows sud)))) in expression: { k <- (whichrow sud); n <- (whereisnothing (head (drop (k - 1) (rows sud)))); (return (k, n)) } sudoku.hs:161:24: couldn't match expected type `int' actual type `(int, t0)' relevant bindings include n :: t0 (bound @ sudoku.hs:160:16) in first argument of `return', namely `(k, n)' in stmt of 'do' block: (return (k, n)) in expression: { k <- (whichrow sud); n <- (whereisnothing (head (drop (k - 1) (rows sud)))); (return (k, n)) }
edit again: pretty sure function behave funny if there no blank cells..
first, utilize tail-recursive, not recursive functions.
whichrow :: sudoku -> int whichrow s = whichrow' s 0 -- tail-recursive whichrow' (sudoku (x:xs)) = if isnothingpresent x 1 else whichrow' (sudoku xs) (i+1)
second, don't forget @ cases @ pattern matching:
whichrow :: sudoku -> int whichrow s = whichrow' s 0 whichrow' (sudoku []) = -- missing case whichrow' (sudoku (x:xs)) = if isnothingpresent x 1 else whichrow' (sudoku xs) (i+1)
third, why utilize many parentheses?
blank :: sudoku -> pos blank sud = k <- whichrow sud n <- whereisnothing (head (drop (k-1) (rows sud))) homecoming (k, n)
but main - code invalid. do
monad, utilize int
only. utilize let
instead
blank :: sudoku -> pos blank sud = allow k = whichrow sud in allow n = whereisnothing $ head $ drop (k-1) (rows sud) in (k, n)
or where
blank :: sudoku -> pos blank sud = (k, n) k = whichrow sud n = whereisnothing $ head $ drop (k-1) (rows sud)
fourth, don't reuse lot of bool
, if smth == true ....
, write instead if smth ...
. let's at
isnothingpresent :: block -> bool isnothingpresent b | nil `notelem` b = false | otherwise = true
we rewrite to
isnothingpresent :: block -> bool isnothingpresent b = not $ nil `notelem` b
or, more simply
isnothingpresent :: block -> bool isnothingpresent b = nil `elem` b
haskell sudoku
No comments:
Post a Comment