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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:31:59+00:00 2026-05-29T06:31:59+00:00

In this Haskell code the constructor arguments(length and width) can be of any data

  • 0

In this Haskell code the constructor arguments(length and width) can be of any data type (like Int,Char etc.). Is there a way by which i can explicitly specify the data type of length and width? How can i specify the return type of function getLength?

{-# LANGUAGE EmptyDataDecls, DeriveDataTypeable, TemplateHaskell #-}
{-# OPTIONS_GHC -fcontext-stack=100 #-}

module Rectangle where

import OOHaskell

$(label "getLength")
$(label "getWidth")
$(label "incr")
$(label "lengthenBy")
$(label "setLength")
$(label "setWidth")
$(label "show'")

rectangle length width self
 = do
     lengthRef <- newIORef length
     widthRef <- newIORef width
     return $
          getLength      .=. readIORef lengthRef
      .*. getWidth      .=. readIORef widthRef
      .*. setLength      .=. writeIORef lengthRef
      .*. setWidth      .=. writeIORef widthRef
      .*. lengthenBy    .=. (\dl ->
              do
             length <- self # getLength
             (self # setLength) (length + dl))
      .*. incr          .=. (self # lengthenBy) (1)
      .*. show'         .=. printLn ("Length : "<< self # getLength<<" Width : "<< self # getWidth)
      .*. emptyRecord

The main should work (which it is) if it is written like:

main = do
   c1 <- mfix $ rectangle 0 0
   c2 <- mfix $ rectangle 0 0
   c3 <- mfix $ rectangle 0 0
   c1# setWidth $ 3
   c2# setWidth $ 2
   c3# setWidth $ 2
   c1# incr
   c2# incr
   c1# incr
   c1# show'
   c3# show'

But main is working even if it is written this way which is undesirable( as width cannot be a character).

main = do
   c1 <- mfix $ rectangle 0 'a'
   c2 <- mfix $ rectangle 0 'b'
   c3 <- mfix $ rectangle 0 'c'
   c1# setWidth $ 'd'
   c2# setWidth $ 'e'
   c3# setWidth $ 'f'
   c1# incr
   c2# incr
   c1# incr
   c1# show'
   c3# show'
  • 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-29T06:32:00+00:00Added an answer on May 29, 2026 at 6:32 am

    Assuming this is all type-safe, the return type of getLength will match the contents of lengthRef, so you just need to add a type signature somewhere that constrains that type, and the type of getLength should be inferred correctly from that.

    For example, you can specify the type when creating lengthRef:

    lengthRef <- newIORef (length :: Int)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr
I have the following Haskell code import Data.Int import System.Environment type Coord = (Int16,
I've come across a piece of Haskell code that looks like this: ps@(p:pt) What
I'm trying to put this data type in a Haskell Set, but I don't
I am attempting to create a type synonym that looks something like this: data
Recently I was introduced to this OCaml code which in Haskell can be written
In the following Haskell code, how can this be written more succinctly? Is it
As in this code: import Data.Char groupsOf _ [] = [] groupsOf n xs
I'm struggling with ambiguous type variables in typeclasses in Haskell: This code boils down
This question is for the people who know both Haskell (or any other functional

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.