Error de alcance en OOH Ejemplo de Askell Problema

I have tried to implement the rectangle problem using OOHaskell.

{-# 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")

rectangle length width self
          = do
             lengthRef <- newIORef value
              widthRef <- newIORef width
              return $
              getLength   .=. readIORef lengthRef
          .*. getWidth    .=. readIORef widthRef
          .*. setLength   .=. writeIORef lengthRef
          .*. setWidth    .=. writeIORef widthRef
          .*. lengthenBy  .=. (\dv ->
                 do
                   value <- self # getValue
                   (self # setValue) (value + dv))
          .*. incr        .=. (self # (lengthenBy 1))
          .*. emptyRecord

But i am getting the scope error. The error meassage is

 Rectangle.hs:21:38: Not in scope: `widthRef'
 Rectangle.hs:22:39: Not in scope: `lengthRef'
 Rectangle.hs:23:39: Not in scope: `widthRef'

¿Cómo puedo solucionar el error?

Thanks Daniel done that. But now the error i am getting is:

The function `lengthenBy' is applied to one argument,
but its type `Proxy LengthenBy' has none
In the second argument of `(#)', namely `(lengthenBy 1)'
In the second argument of `(.=.)', namely `(self # (lengthenBy 1))'
In the first argument of `(.*.)', namely
  `incr .=. (self # (lengthenBy 1))'

preguntado el 02 de febrero de 12 a las 11:02

¿Qué es? the rectangle problem? -

Problem in which there is a simple rectangle and some basic functions that operate on it.. -

1 Respuestas

Fix your indentation:

      = do
         lengthRef <- newIORef value
          widthRef <- newIORef width

the 'l' of lengthRef and the 'w' of widthRef must be in the same column.

As it stands, it is parsed as

= do lengthRef <- newIORef value widthRef <- newIORef ...

However, I think that should produce a parse error and not reach the 'not in scope' phase. So I guess that's not in your actual code but a paste-glitch here.

Entonces:

          return $
          getLength   .=. readIORef lengthRef
      .*. getWidth    .=. readIORef widthRef
      .*. setLength   .=. writeIORef lengthRef
      .*. setWidth    .=. writeIORef widthRef
      .*. lengthenBy  .=. (\dv ->
             do
               value <- self # getValue
               (self # setValue) (value + dv))
      .*. incr        .=. (self # (lengthenBy 1))
      .*. emptyRecord

breaks it, the .*. is indented less than the do-block indentation, so it's parsed as

(do ...
    getLength .=. readIORef lengthRef) .*. getWidth .=. ...

Indent the stuff you want to go inside the return farther than the enclosing do-block.

Respondido 02 Feb 12, 15:02

Never used OOHaskell, so I don't know what everything's type is, but from the error message, a possible cause is that it should be ((self # lengthenBy) 1) en lugar de (self # (lengthenBy 1)). - Daniel Fischer

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.