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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:03:15+00:00 2026-05-22T12:03:15+00:00

I have managed to make some progress in this part of my assignment but

  • 0

I have managed to make some progress in this part of my assignment but have attached part of the code below that I have made:

module Grid where

data State = On | Off deriving (Eq, Show)


next :: State -> State
next On = Off
next Off = On

type Row = [State]
type Grid = [[State]]
type Point = (Int,Int)

initialRow      :: Int -> Row
initialRow w    = replicate w Off


updateRow :: Row -> Int -> Row
updateRow  (r:rs) x 
    | x==0        = next r:rs
    | otherwise   = r : (updateRow rs (x-1))

update :: Grid -> Point -> Grid
update [[]] (x,y)       =   [[]]
update [(g:gs)] (x,y)   =   [(updateRow (g:gs) x)]

As shown in the last line just above, I have managed to get update to work for when x = any Int as shown below (with the xth element inverted) – ghci.

*Grid> update [[Off,Off,Off,Off]] (2,0)
[[Off,Off,On,Off]]
*Grid> 

It all comes unstuck however when I try working with multiple lists such as this, or select a certain list within the list to update the xth element:

*Grid> update [[Off,Off,Off,Off],[Off,Off,Off,Off]] (2,0)
*** Exception: Grid.hs:(24,0)-(25,47): Non-exhaustive patterns in function update

I can’t seem to ‘genralise’ a formula in this function.

I also MUST follow THIS type convention:

updateRow :: Grid -> Point -> Grid

Basically, what I would like to do is update from something like this…

[[Off,Off,Off,Off],
 [Off,Off,Off,Off],
 [Off,Off,Off,Off],
 [Off,Off Off,Off]]

to this:

[[Off,Off,Off,Off],
 [Off,Off,**On**,Off],
 [Off,Off,Off,Off],
 [Off,Off Off,Off]]

where ‘x’ is the value of the element and ‘y’ is the value of the list within the list IYGWIM.

Thanks in advance.

  • 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-22T12:03:16+00:00Added an answer on May 22, 2026 at 12:03 pm
    update :: Grid -> Point -> Grid
    update [[]] (x,y)       =   [[]]
    

    This checks for a list that contains the empty list.

    update [(g:gs)] (x,y)   =   [(updateRow (g:gs) x)]
    

    This checks for a list that contains one list, the latter containing at least one element (bound to the variable g).

    You want to check for a list that contains multiple lists.

    The pattern should look like:

    update :: Grid -> Point -> Grid
    update [[]] (x, y)       = [[]]
    update (row:rows) (x, 0) = updateRow row x : rows
    update (row:rows) (x,y)  = -- I'll let you fill this, notice the 0 on the previous line
    

    Remember a Grid is just a list of Rows.

    The second line now means “if you want to update the 0th line of this grid, then update the first row”, the last line should mean “if you want to update the yth line of this grid, then leave the first one as is, and recursively update the rest of the rows” (of course, y must be changed accordingly in the recursive call).

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

Sidebar

Related Questions

I have created some documents and managed to make some simple queries but I
I have some legacy code that is creating instances of classes. I have managed
I'm a newbie to jquery, but have managed to make a show-function and hover
I am trying out Mechanize to make some routine simpler. I have managed to
I managed to make some arbitrary signals manually, but I want to define them
Possible Duplicate: Replace individual list elements in Haskell? I have managed to make some
I've managed to make some single dimension array lists but I can't figure out
I have an assembly which I have managed to make visible to VB6 and
I am using a timer in xcode i have managed to make it count
with the help of resources of stackoverflow, i have managed to make autocomplete on

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.