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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:30:53+00:00 2026-06-06T23:30:53+00:00

Well I can’t figure out how I could make it in haskell. For example,

  • 0

Well I can’t figure out how I could make it in haskell. For example, I have such matrix

[[1,2],[3,4]]

and I want to generate a list of lists with all possible neighbours of each element for this matrix.

Expected result is:

[[1,2],[1,3],[1,4],[2,1],[2,3],[2,4],[3,1],[3,2],[3,4],[4,1],[4,2],[4,3]]

I know how to make function which will create list of list with cords of neighbours of each cell:

pos how = [ (0+dx,0+dy) | dx <- [0..(how-2)], dy <- [0..how-1] ] :: [(Int,Int)]
neighbour (x,y) how = [ (x+dx,y+dy) | dy <- [-1..1], dx <- [-1..1],
                                      x+dx >= 0, y+dy >= 0, x+dx<=how-2, y+dy <= how-1,
                                      (x,y)/=(x+dx,y+dy) ] :: [(Int,Int)]
all_n how = [ p | x <- pos how, let p = neighbour x how ] :: [[(Int,Int)]]

but I can’t change it to work as I described.

  • 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-06T23:30:54+00:00Added an answer on June 6, 2026 at 11:30 pm

    Let me sketch a very different approach to the one you suggested. When talking about neighbors, I’ll say a d-neighbor of e is the element one step in direction d from element e. So, for example, in the matrix [[1,2],[3,4]], the number 2 is a right-neighbor of number 1. We’ll need to use some library code.

    import Data.List
    

    We’ll start from the very simplest thing: let’s just find right-neighbors of a one-dimensional list.

    rightList (x:y:rest) = (x,y) : rightList (y:rest)
    rightList _ = []
    

    We can find all the right-neighbors in a matrix by nondeterministically choosing a row of the matrix and finding all right-neighbors in that row.

    right m = m >>= rightList
    

    We can take any function for finding neighbors and create the function for finding neighbors in the other direction by reversing the tuples. For example, left neighbors are the same as right neighbors, but with the tuple elements swapped, so:

    swap (x,y) = (y,x)
    co direction = map swap . direction
    left = co right
    

    What about down-neighbors? Actually, down-neighbors are just right-neighbors in the transposed matrix:

    down = right . transpose
    up   = co down
    

    The next direction of interest are down-right-neighbors. This one is a little trickier; what we’re going to do is walk down two lists in parallel, but offset by one.

    downRight (xs:ys:rest) = zip xs (drop 1 ys) ++ downRight (ys:rest)
    downRight _            = []
    upLeft = co downRight
    

    There’s one final direction, namely up-right-neighbors; these are down-right-neighbors if we flip the matrix upside down.

    upRight  = downRight . reverse
    downLeft = co upRight
    

    Finally, to form all neighbors, we can nondeterministically choose a neighboring direction, then find all neighbors in that direction.

    allDirections = [right, left, up, down,
                     downRight, upLeft, upRight, downLeft]
    neighbors m = allDirections >>= ($m)
    

    The output isn’t in exactly the order you specified, but I imagine this may not matter so much.

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

Sidebar

Related Questions

I have a menu working fairly well but can't figure out how to make
Well I can't figure this one out... I have this Wordpress I use as
I want to make my url to support http and https as well,Can you
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
well I want to debug some script with Firebug, (cause I can't see anything
well basically can long long int hold number with 100 symbols? for example can
Hello Those who have used WatiN likely also used DialogHandlers . Well can someone
I have done .NET Win32 projects and from Java well I can adopt my
I want to add items in an SVG via jQuery. I can well insert
Well I can start the thread when I press togglebutton to ON and it

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.