Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would start off by noting that Haskell, to say the least, does <em>not</em> encourage traditional OO-style development; instead, it has features and characteristics that lend themselves well to the sort of 'pure functional' manipulation that you won't really find in many other languages; the short on this is that trying to 'bring over' concepts from other (traditional languages) can often be a very bad idea.</p> <blockquote> <p>but I'm trying to encapsulate the behavior into a class</p> </blockquote> <p>Hence, my first major question that comes to mind is why? Surely you must want to <em>do</em> something with this (traditional OO concept of a) class?</p> <p>If an approximate answer to this question is: "I'd like to model some sort of data construct", then you'd be better off working with something like</p> <pre><code>data A = A { xval :: Int } &gt; let obj1 = A 5 &gt; xval obj1 5 &gt; let obj2 = obj1 { xval = 10 } &gt; xval obj2 10 </code></pre> <p>Which demonstrates pure, immutable data structures, along with 'getter' functions and destructive updates (utilizing record syntax). This way, you'd do whatever work you need to do as some combination of functions mapping these 'data constructs' to new data constructs, as appropriate.</p> <p>Now, if you <em>absolutely</em> needed some sort of model of State (and indeed, answering this question requires a bit of experience in knowing exactly <strong>what local versus global state is</strong>), <em>only then</em> would you delve into using the State Monad, with something like:</p> <pre><code>module StateGame where import Control.Monad.State -- Example use of State monad -- Passes a string of dictionary {a,b,c} -- Game is to produce a number from the string. -- By default the game is off, a C toggles the -- game on and off. A 'a' gives +1 and a b gives -1. -- E.g -- 'ab' = 0 -- 'ca' = 1 -- 'cabca' = 0 -- State = game is on or off &amp; current score -- = (Bool, Int) type GameValue = Int type GameState = (Bool, Int) playGame :: String -&gt; State GameState GameValue playGame [] = do (_, score) &lt;- get return score playGame (x:xs) = do (on, score) &lt;- get case x of 'a' | on -&gt; put (on, score + 1) 'b' | on -&gt; put (on, score - 1) 'c' -&gt; put (not on, score) _ -&gt; put (on, score) playGame xs startState = (False, 0) main = print $ evalState (playGame "abcaaacbbcabbab") startState </code></pre> <p>(shamelessly lifted from <a href="http://www.haskell.org/haskellwiki/State_Monad" rel="nofollow">this tutorial</a>). Note the use of the analogous 'pure immutable data structures' <em>within</em> the context of a state monad, in addition to 'put' and 'get' monadic functions, which facilitate access to the state contained within the State Monad.</p> <p>Ultimately, I'd suggest you ask yourself: what is it that you really want to accomplish with this model of an (OO) class? Haskell is not your typical OO-language, and trying to map concepts over 1-to-1 will only frustrate you in the short (and possibly long) term. This should be a standard mantra, but I'd highly recommend learning from the book <a href="http://book.realworldhaskell.org/read/" rel="nofollow">Real World Haskell</a>, where the authors are able to delve into far more detailed 'motivation' for picking any one tool or abstraction over another. If you were adamant, you could model traditional OO constructs in Haskell, but I wouldn't suggest going about doing this - unless you have a <em>really</em> good reason for doing so.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload