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 6905503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:11:28+00:00 2026-05-27T08:11:28+00:00

I’m going through a Haskell tutorial and am given this piece of code to

  • 0

I’m going through a Haskell tutorial and am given this piece of code to do with moving a knight in chess:

import Control.Monad

type KnightPos = (Int,Int)

moveKnight :: KnightPos -> [KnightPos]  
moveKnight (c,r) = do  
    (c',r') <- [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1)  
               ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)  
               ]  
    guard (c' `elem` [1..8] && r' `elem` [1..8])
    return (c',r')

in3 :: KnightPos -> [KnightPos]
in3 start = return start >>= moveKnight >>= moveKnight >>= moveKnight

canReachIn3 :: KnightPos -> KnightPos -> Bool
canReachIn3 start end = end `elem` in3 start 

An exercise is to modify the functions so that canReachIn3 tells you what moves you can take to get to the end position if it is possible to get there.

This tutorial has basically no exercises so I’m having trouble with basic stuff like this…I was thinking of changing the return values of all 3 functions to [[KnightPos]] where 1 big list contains a list for every possible ordering of moves. That would probably then involve moveKnight having a [KnightPos] parameter instead of a KnightPos one, which would then defeat the whole point of monads right?

Any help/thoughts would be greatly appreciated, thanks.

  • 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-05-27T08:11:29+00:00Added an answer on May 27, 2026 at 8:11 am

    It might help to step back from the Monad concept for a bit when thinking about this code, if you find that plain old list operations are more natural for you. So you can rewrite the example code (with a little bit of cleanup for legibility) as:

    type KnightPos = (Int,Int)
    
    moveKnight :: KnightPos -> [KnightPos]  
    moveKnight (c,r) = filter legal candidates where
        candidates = [(c+2,r-1), (c+2,r+1), (c-2,r-1), (c-2,r+1),
                      (c+1,r-2), (c+1,r+2), (c-1,r-2), (c-1,r+2)]
        legal (c',r') = c' `elem` [1..8] && r' `elem` [1..8]
    
    in3 :: KnightPos -> [KnightPos]
    in3 start = concatMap moveKnight (concatMap moveKnight (moveKnight start))
    
    canReachIn3 :: KnightPos -> KnightPos -> Bool
    canReachIn3 start end = end `elem` in3 start
    

    The secret sauce is in concatMap. As you probably know already, it’s synonymous with >>= in the List monad, but it might be more helpful right now to think of it as mapping a function of type KnightPos -> [KnightPos] over a list [KnightPos] to yield a list of lists [[KnightPos]], and then flattening the result back into a single list.

    Okay, so now that we’ve dispensed with monads for the moment, let’s look back at the puzzle… Let’s say your initial KnightPos is (4,4), and you want to track all possible sequences of moves from that position. So define another type synonym:

    type Sequence = [KnightPos]
    

    Then you’d want moveKnight to operate on these sequences, finding all possible moves from the last position in the sequence:

    moveKnight :: Sequence -> [Sequence]
    

    So starting from a sequence [(4,4)], we’d get the list of sequences: [[(4,4), (6,3)], [(4,4), (6,5)], [(4,4), (2,3)], ... ]. Then I think the only change you’d need to make to in3 is to fix its type signature accordingly:

    in3 :: Sequence -> [Sequence]
    

    I don’t think the actual implementation changes. Finally, you’ll want canReachIn3 to look something like:

    canReachIn3 :: KnightPos -> KnightPos -> [Sequence]
    

    I’m leaving the implementation detail out here on purpose since I don’t want to ruin the puzzle for you entirely, but I’m hoping I’ve illustrated the point here that there isn’t anything particularly “special” about a list, or a list of lists, or whatever. All we’ve really done is substituted a function of type KnightPos -> [KnightPos] with a new function of type Sequence -> [Sequence] — pretty much the same shape. So fill in the implementations of each function using whatever style feels natural, and then once you have it working, go back to the monadic style, replacing concatMap with >>= and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.