2008/12/16 Mozhgan Kabiri <span dir="ltr">&lt;<a href="mailto:mozhgan_kch@hotmail.com">mozhgan_kch@hotmail.com</a>&gt;</span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">




<div>
<span style="color: rgb(0, 0, 0); font-family: &#39;times new roman&#39;; font-size: 11px;"><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">Hi ..

Hope you are doing well . I&#39;ve just joined this group.</span></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;"> 
Recently, I am  struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable .</span></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">
I tried to write a simple program, for example calculationg &#39;n=n+1&#39; few times.And then I tried to do it in parallel by using &#39;par&#39; and &#39;pseq&#39; . The aim was to get the wrong answer because we have to share a variable here,and without using &#39;MVar&#39; function we will get the wrong answer for the calculation .</span></div>
</span></div></blockquote><div><br>This is fortunately impossible.&nbsp; par can never change the semantics of a program; it just says &quot;compute this in parallel now because we might need it later&quot;, as opposed to just computing it later when it is demanded.&nbsp; Because Haskell is referentially transparent, the answer it will get now vs. later will always be the same.<br>
<br>Race conditions cannot happen with par.&nbsp; Perhaps you want to experiment with concurrency rather than parallelism (this is the realm in which MVars lie).&nbsp; In that case, look at the function forkIO, which spawns a new thread, and the MVar operations.<br>
<br>Luke<br><br><br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><span style="color: rgb(0, 0, 0); font-family: &#39;times new roman&#39;; font-size: 11px;"><div style="margin: 0px;">
<span style="font-family: Tahoma; white-space: pre-wrap;"></span></div><div style="margin: 0px;"><font size="3" face="Tahoma"><span style="font-size: 11px; white-space: pre-wrap;"><br></span></font></div><div style="margin: 0px;">
<span style="font-family: Tahoma; white-space: pre-wrap;">I don&#39;t know how to write it in parallel in order to get a wrong answer when we don&#39;t use MVar,because we have a shared variable here. I read about MVars as well,but also I don&#39;t know how to combine MVar and Par together to get the program to work.</span></div>
<div style="margin: 0px;"><font size="3" face="Tahoma"><span style="font-size: 11px; white-space: pre-wrap;"><br></span></font></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">I wrote this :</span></div>
<div style="margin: 0px;"><font size="3" face="Tahoma"><span style="font-size: 11px; white-space: pre-wrap;"><br></span></font></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">module Main where

f :: Int -&gt; Int -&gt; Int
f i n = g 1 i n
  where g x i n | x &lt;= i = g (x+1) i (n+1)
                | otherwise = n

main :: IO ()
main =
  do putStrLn &quot;starting...&quot;
     let r = f 10 5
     putStrLn (show r)
     putStrLn &quot;finished&quot;
<br></span></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">I want to make to work in parallel by using &#39;Par&#39;.And also use MVar for this simple example to work.</span></div>
<div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">All of the example about MVar are a little bit complicated and I couldn&#39;t figure it that how can I write one,the same !</span></div><div style="margin: 0px;">
<font size="3" face="Tahoma"><span style="font-size: 11px; white-space: pre-wrap;"><br></span></font></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">Can any one help me with this ? I want a simple example that I can feel the need of MVar when I run my program in parallel and while I am using a shared variable.</span></div>
<div style="margin: 0px;"><br></div><div style="margin: 0px;"><span style="font-family: Tahoma; white-space: pre-wrap;">Regards;
Mozhgan
</span></div><div><span style="font-family: Tahoma; white-space: pre-wrap; font-size: 8pt;"><br></span></div></span><br><hr>Get news, entertainment and everything you care about at Live.com. <a href="http://www.live.com/getstarted.aspx" target="_blank">Check it out!</a></div>

<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>