Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:55:44+00:00 2026-06-15T02:55:44+00:00

Beneath this text, you can see some code for creating Arukone puzzles. But there

  • 0

Beneath this text, you can see some code for creating Arukone puzzles.
But there is a problem: the polymorphic data-type Puzzle a represents an Arukone puzzle with the labels of the type a.

But I would like to have a puzzle-type with the instance of Show class (without using deriving Show.)
The show function would be enough, showsPrec isn’t necessary.

So do you know how this works? I want to define Show for all the types a which are an instance of the following ToChar class?

class ToChar a where
 toChar :: a -> Char

instance ToChar Char where
 toChar = id

instance ToChar Int where
 toChar = head . show

Code :

import Data.Maybe (listToMaybe)

data Size  = Size Int Int deriving (Eq, Show)
data Pos   = Pos  Int Int deriving (Eq, Show)
data Link l = Link l Pos Pos
data Puzzle l = Puzzle Size [Link l]

assign :: Int -> a -> [a] -> [a]
assign 0 v (_:vs) = v:vs
assign n v (v’:vs) = v’: assign (n - 1) v vs

assign2 :: Int -> Int -> a -> [[a]] -> [[a]]
assign2 r c v vs = assign r (assign c v (vs !! r)) vs

assignPos :: Pos -> a -> [[a]] -> [[a]]
assignPos (Pos r c) = assign2 (r-1) (c-1)

tabulate :: Int -> (Int -> a) -> [a]
tabulate 0 _ = []
tabulate i f = f 0 : tabulate (i - 1) (f . (+1))

tabulate2 :: Int -> Int -> (Int -> Int -> a) -> [[a]]
tabulate2 b h f = tabulate h (\r -> tabulate b (\c -> f r c))

tabulatePos :: Size -> (Pos -> a) -> [[a]]
tabulatePos (Size h b) f = tabulate2 b h (\r c -> f (Pos (r +1) (c + 1)))

showPuzzle :: Puzzle a -> [[Maybe a]]
showPuzzle (Puzzle sz links) = tabulatePos sz findPosMaybe
  where findPosMaybe pos =  -- edit: findPosMaybe needs to be further left than the next line
          listToMaybe [l | Link l pos1 pos2 <- links, pos1 == pos || pos2 == pos]

Solutions and Input

*Main> show (Puzzle (Size 2 3) [Link 1 (Pos 1 1) (Pos 1 3), Link 2 (Pos 2 1) (Pos 2 3)])

"1 1\n2 2"


*Main> show (Puzzel (Size 5 5) [Link ’a’ (Pos 3 1) (Pos 4 3), Link ’b’ (Pos 5 1) (Pos 1 5), Link ’c’ (Pos 2 5) (Pos 5 5), Link ’d’ (Pos 4 1) (Pos 2 2)])

"    b\n d  c\na    \nd a  \nb   c"          <-- result -- remarks the spaces

putStrLn of last example

    b
 d  c
a 
d a 
b   c

All help is welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:55:45+00:00Added an answer on June 15, 2026 at 2:55 am

    First let’s use the ToChar class to turn the Maybe elements into spaces or values:

    maybeToString :: ToChar a => Maybe a -> String
    maybeToString Nothing = " "
    maybeToString (Just x) = toChar x : ""
    

    Now we can make the Show instance:

    instance ToChar a => Show (Puzzle a) where
      show = unlines . map (concatMap maybeToString) . showPuzzle
    

    It’s not clear to me whether you wanted padding or not, so you could alter your maybeToString to say " " and toChar x : " " if you want more space.

    With the definitions above we get:

    *Main> show (Puzzle (Size 2 3) [Link (1 :: Int) (Pos 1 1) (Pos 1 3), Link 2 (Pos 2 1) (Pos 2 3)]) 
    "1 1\n2 2\n"
    *Main> putStrLn $ show (Puzzle (Size 2 3) [Link (1 :: Int) (Pos 1 1) (Pos 1 3), Link 2 (Pos 2 1) (Pos 2 3)]) 
    1 1
    2 2
    

    Example 2:

    *Main> show (Puzzle (Size 5 5) [Link 'a' (Pos 3 1) (Pos 4 3), Link 'b' (Pos 5 1) (Pos 1 5), Link 'c' (Pos 2 5) (Pos 5 5), Link 'd' (Pos 4 1) (Pos 2 2)])
    "    b\n d  c\na    \nd a  \nb   c\n"
    *Main> putStr it
        b
     d  c
    a    
    d a  
    b   c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry I can't put this code live anywhere at the moment but hopefully the
I have this css http://jsfiddle.net/thiswolf/3GYY4/ and beneath each text input,i want to have some
CSS/HTML problem. This is crazy - I can only assume that I am an
I am facing error C2065: 'IOCTL_NDISUIO_OPEN_DEVICE' : undeclared identifier error in beneath code. Where
I am working on an iPhone view that has some header text along with
In this website: http://theoew.uuuq.com/portfolios/Idea/ I use Cufon to style the text for the the
I am hoping that someone can help me with a problem I've got at
Can someone tell me where to add the small piece of code to have
[Update] I actually compromised on this problem for now by foregoing the fixed footer
I think I searched thoroughly this site, but could not find answer to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.