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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:57:54+00:00 2026-05-28T19:57:54+00:00

I am working on this program for better understanding of haskell and state monad.

  • 0

I am working on this program for better understanding of haskell and state monad. I made a module Rectangle which has data type Rectangle and a function to increment the length of the rectangle. To increment the length i made a function plus. Could there have been a better way than this plus function? Also while incrementing i am doing addition with a new rectangle which is logically incorrect. How to perform the increment in the correct way? Is there any way in which instead of giving the default width the compiler prompts the user for width?
How can i sort of extract one or more rectangle or any attribute(length or breadth) while doing stateful computation?

{-# LANGUAGE TemplateHaskell, TypeOperators #-}
module Rectangle(Rectangle(Rectangle),GlobState(GlobState),plus,newGlobState,r1,r2,r3,incr) where

import Control.Monad.State hiding (modify)
import Data.Label (mkLabels)
import Data.Label.Pure ((:->))
import Data.Label.PureM

type Length = Int
type Width = Int

data Rectangle = Rectangle Length Width deriving (Eq,Read,Show)

data GlobState = GlobState { _r1 :: Rectangle , _r2 :: Rectangle , _r3 :: Rectangle } deriving Show
$(mkLabels [''GlobState])

plus :: Rectangle -> Rectangle -> Rectangle
plus (Rectangle x z) (Rectangle y w) = Rectangle (x+y) z

newGlobState:: GlobState
newGlobState = GlobState { _r1 = Rectangle 0 10, _r2 = Rectangle 0 10, _r3 = Rectangle 0 10}

incr :: (GlobState :-> Rectangle) -> State GlobState ()
incr x = modify x (plus (Rectangle 1 10))

I have made a new module for selecting one of the rectangles:

{-# LANGUAGE TemplateHaskell, TypeOperators #-}
module ChooseRect(ChooseRect(ChooseRect),makeChoice,select) where

import Rectangle
import Control.Monad.State hiding (modify)
import Data.Label (mkLabels)
import Data.Label.Pure ((:->))
import Data.Label.PureM

type Choice = Int

data ChooseRect = ChooseRect Rectangle Rectangle deriving Show

makeChoice:: Rectangle-> Rectangle->ChooseRect
makeChoice p1 p2 = ChooseRect p1 p2

select:: ChooseRect -> Choice -> Rectangle
select (ChooseRect (Rectangle x z) (Rectangle y w)) choice = if (choice==1)
                then let selectedRectangle = Rectangle x z
                in selectedRectangle
                else let selectedRectangle = Rectangle y w
                in selectedRectangle

But when i made changes to main module i m getting error.

module Main where

import Rectangle
import ChooseRect
import Control.Monad.State hiding (modify)
import Data.Label (mkLabels)
import Data.Label.Pure ((:->))
import Data.Label.PureM

main :: IO ()
main = do
    let x = flip execState newGlobState $ do
        incr r1
        incr r2
        incr r1
        incr r3
        incr r3
    let y=makeChoice r1 r2
    print y
    print x

The error message is

Couldn't match expected type `Rectangle'
            with actual type `Data.Label.Abstract.Lens
                                (~>0) GlobState Rectangle'
In the first argument of `makeChoice', namely `r1'
In the expression: makeChoice r1 r2
In an equation for `y': y = makeChoice r1 r2

Please explain the error and how to remove it

  • 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-28T19:57:55+00:00Added an answer on May 28, 2026 at 7:57 pm
    1. Better than your plus would be a lengthenBy :: Length -> Rectangle -> Rectangle.

      Oh, and you’re using modify wrongly… you need to use the modify from Control.Monad.State and
      the modify from Data.Label.Pure.

      incr x = Control.Monad.State.modify $ Data.Label.Pure.modify x (lengthenBy 1)
      
    2. See the functions getLine :: IO String (for getting a line of input) and read :: String -> Int or reads :: String -> [(Int, String)] (for turning it from a String into an Int; the former is simpler to use but crashes if it can’t parse the input) (and putStr :: String -> IO () or putStrLn :: String -> IO () for showing a prompt).

      n.b. Do your i/o in the outermost do-block in main, and pass the results as extra parameters into your
      stateful computation.

    3. Replace

      makeChoice r1 r2
      

      with

      makeChoice (get r1 x) (get r2 x)
      

      Look at the types:

      r1, r2 :: GlobState :-> Rectangle
      makeChoice :: Rectangle -> Rectangle -> ChooseRect
      

      So r1 and r2 are not of the right type to pass to makeChoice.

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

Sidebar

Related Questions

I need to understand the working of this particular program, It seems to be
I am working on a bug in this program where it should be able
My friend and I are working on a program. This program is going to
This is the last part of the program I am working on. I want
I'm a LAMP guy and ended up working this small news module for an
I have this working great, but I'd like a deeper understanding of what is
I've been working on this elevator program for quite some time now and finally
I'm working on a program that shall have an updates module (online). I can't
I am working on a simple program that collects data from the field on
I have a program which consists of multiple projects in eclipse (working under ubuntu

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.