
I want to learn more about Haskell and I started with my Sudoku solver class SudokuClass s where initAreas :: s -> s createEmptySudoku :: s getBoardLength :: s -> Int setBoard :: s -> Board -> s getCellToAreas :: s ->CellToAreas ... other functions with default implementations I created a Sudoku solver for 9x9 sudoku's. data Sudoku9x9 = Sudoku9x9 { board :: Board, areas:: Areas, cellToAreas :: CellToAreas } deriving (Show) Board, Areas and CellToAreas are of type Map. After that I create an instance: instance SudokuClass Sudoku9x9 where initAreas s = ..... After implementing the 9x9 solver I wanted to create a second instance SudokuTwins, that solves a different kind of sudoku, but uses the same record: data SudokuTwins = SudokuTwins { board :: Board, areas:: Areas, cellToAreas :: CellToAreas } deriving (Show) This is almost a copy of Soduku9x9 (right side of the =) How can I express the fact that all sudoku solvers use the same recordtype? Can I make a type of the record structure {board:: Board.... } and use it in the constructor function or can I incorperate the recordtype in the class or ......? Kees

On Jun 17, 2011, at 7:00 AM, Kees Bleijenberg wrote:
How can I express the fact that all sudoku solvers use the same recordtype? Can I make a type of the record structure {board:: Board.... } and use it in the constructor function or can I incorperate the recordtype in the class or ......?
This is a good use for the "newtype" declaration. Define your SudokuRecord and then specialize it using newtype. data SudokoRecord ... newtype SudokuTwins = SudokuTwins SudokuRecord newtype Sudoku9x9 = Sudoku9x9 SudokuRecord instance SudokuClass SudokuTwins where ... instance SudokuClass Sudoku9x9 where ... ____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com
participants (2)
-
David Place
-
Kees Bleijenberg