Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: rational exponents (Brandon Allbery)
2. Re: rational exponents (Daniel Fischer)
3. Re: Help me with designing my daemon, please. (Michael Litchard)
4. Re: Help me with designing my daemon, please. (Michael Litchard)
5. Question about lazy evaluation (Zhi-Qiang Lei)
----------------------------------------------------------------------
Message: 1
Date: Wed, 7 Sep 2011 17:38:10 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] rational exponents
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<CAKFCL4WMX_-YQpgoEyY-E7v6V+-iiR1Ms1A=hxzycambuno...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Sep 7, 2011 at 17:32, Christopher Howard <
[email protected]> wrote:
> Or maybe an all-rational math? I'm mainly curious as my CASIO fx-115W
> calculator returns a result of exactly 4 if I input 8^(2/3), but I do not
> know how it arrives at the answer.
>
Calculators often resort to built-in tables.
The easiest solution is probably to round off the last 2 digits of the full
precision.
--
brandon s allbery [email protected]
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110907/f7d9cfc9/attachment-0001.htm>
------------------------------
Message: 2
Date: Wed, 7 Sep 2011 23:44:50 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] rational exponents
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-1"
On Wednesday 07 September 2011, 23:32:28, Christopher Howard wrote:
> Are there any alternative approaches that could taken? Perhaps, some
> kind of Floating Ratio implementation?
That's not sanely possible. Floating demands pi and functions like log,
exp, sin... which usually have irrational values. Since Rational contains
no "closest value to the true result", a Floating instance for Rational
would be very evil.
>
> Prelude Data.Ratio> 8 ** (2 % 3)
>
> <interactive>:1:3:
> No instance for (Floating (Ratio a0))
> arising from a use of `**'
> Possible fix: add an instance declaration for (Floating (Ratio a0))
> In the expression: 8 ** (2 % 3)
> In an equation for `it': it = 8 ** (2 % 3)
>
> Or maybe an all-rational math?
That's possible to a certain extent, but in most cases powers with non-
integer exponents will again be irrational, so those couldn't be done.
> I'm mainly curious as my CASIO fx-115W
> calculator returns a result of exactly 4 if I input 8^(2/3), but I do
> not know how it arrives at the answer.
Probably it's something like
Prelude> 8 ** (2/3) :: Float
4.0
With the right number of bits in the type, the errors cancel for certain
calculations.
------------------------------
Message: 3
Date: Wed, 7 Sep 2011 15:21:11 -0700
From: Michael Litchard <[email protected]>
Subject: Re: [Haskell-beginners] Help me with designing my daemon,
please.
To: David McBride <[email protected]>
Cc: [email protected]
Message-ID:
<caezekyp9aqxumjjibc11r0ykx4f91yw9hh6q6whu_2j8a3k...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
I need to provide more detail. Keep in mind that what I want to do,
and the way I'm doing it may not fit at all. This code is using Chans
and MVars, but could be using TChans and TVars easily enough. I kept
things the way they are to just express what I'm trying to do
> data ServerModel = PRODUCT1
> | PRODUCT2
> | PRODUCT3
> | PRODUCT4
> | PRODUCT5
> deriving Show
> newtype ProcessStep = PStep (ServerModel -> FilePath -> IO ())
> type Cookie = String
> type ProcessState = MVar ([ProcessStep],ProcessConfig)
> data ProcessConfig = PConfig { model :: ServerModel
> , ipAddress :: String
> , rootDirectory :: FilePath
> , cookie :: Cookie
> } deriving Show
> preProcess :: ServerModel -> FilePath -> IO ()
> preProcess sModel fPath = putStrLn ("preProcessing" ++ show sModel)
> initiatedJob :: ServerModel -> FilePath -> IO ()
> initiatedJob sModel fPath = putStrLn ("in progress" ++ show sModel)
> makeChart :: ServerModel -> FilePath -> IO ()
> makeChart sModel fPath = putStrLn ("chart making" ++ show sModel)
main :: IO ()
main = do
pState <- make
world <- newEmptyMVar :: IO (ProcessState)
worldQueue <- newChan :: IO (Chan ProcessState)
installHandler userDefinedSignal2 (Catch $ worldHandler world worldQueue) N
othing
installHandler userDefinedSignal1 (Catch $ emptyQueue worldQueue
) Nothing ---- This is for testing purposes. I just want to be able
to empty the Chan and see if expected behavior holds
installHandler nullSignal (Catch $ emptyMVar world ) Nothing
---- For testing as well, same reason as above.
sequence_ $ repeat $ queueCheck
> worldHandler :: ProcessState -> Chan ProcessState -> IO ()
> worldHandler world worldQueue = do
>
> mvarState <- isEmptyMVar world
> let tStep = PStep undefined
> let tConfig = PConfig { model = undefined,
> ipAddress = undefined,
> rootDirectory = undefined,
> cookie = undefined
> }
>
> let tState = undefined
> case (mvarState) of
> True -> putMVar world tState
> False -> growQueue
> where growQueue = do
> newWorld <- newMVar ([tStep], tConfig)
> writeChan worldQueue newWorld
On Wed, Sep 7, 2011 at 1:37 PM, David McBride <[email protected]> wrote:
> I'm imagining this:
>
> data TestInfo = {
> ?testname :: String,
> ?etc..
> }
> data TestResult = {
> ?success :: Bool,
> ?etc...
> }
>
> data Test = Test (TestInfo -> IO ())
> type Tests = [Test]
>
> main = do
> ?let tests = [whatever] :: Tests
> ?testchan <- newTChanIO :: IO (TChan TestInfo)
> ?resultchan <- newTChanIO :: IO (TChan TestResult)
> ?exceptionwhatever $ queuetest testchan
> ?forkIO $ testThread (testchan,resultchan) tests
> ?printTestResults resultChan
>
> queuetest chan = atomically $ writeTchan (TestInfo .....)
>
> testThread (testchan, resultchan) tests = forever $ do
> ?newtest <- atomically $ readTChan testchan
> ?results <- mapM tests newTest
> ?atomically $ writeTChan resultchan results
>
> printTestResults chan = forever $ do
> ?x <- atomically $ readTChan chan
> ?print x
>
> Something like that perhaps?
>
>
> On Wed, Sep 7, 2011 at 3:31 PM, Michael Litchard <[email protected]> wrote:
>> This is what I am trying to do.
>> I have tests to run and manage. I'm only running one test at a time.
>> When my daemon gets a signal, it will either prep a test and run it,
>> or queue the request. After it runs the test, I want it to check the
>> queue for other tests that may have been requested.
>> This is my first expedition into this domain. I'm trying to collect
>> MVars and putting tem in a TChan is the way that seemed right, but I'm
>> not sure at all. This is my first guess.
>> I thought I needed a forked thread for the eventuality that I get a
>> signal while my transaction is being executed.
>> Have I clarified or further obfuscated?
>>
>> On Wed, Sep 7, 2011 at 12:22 PM, David McBride <[email protected]> wrote:
>>> It sounds bizarre. ?Why pass around an mvar in tchan, when you could
>>> just pass a maybe around and pattern match to see if it is Nothing or
>>> not? ?Also, why have forkio and tchan at all if they are only going to
>>> operate in sequence, one at a time?
>>>
>>> What exactly are you trying to do?
>>>
>>> On Wed, Sep 7, 2011 at 3:01 PM, Michael Litchard <[email protected]>
>>> wrote:
>>>> I have a daemon I need to build, and need to work out some design
>>>> details I am having difficulty with. Here's what the design looks like
>>>> right now
>>>>
>>>> When the daemon starts it creates an empty MVar and an empty TChan.
>>>> Then it listens for a usrSIG1.
>>>> when it gets one, it checks to see if the MVar is empty. If it is, it
>>>> does some stuff to fill the MVar, which is then used to pass around
>>>> state for a list of functions. These functions are always the same.
>>>> After evaluating these functions, the TChan is checked. As long as the
>>>> TChan has something in it,
>>>> it populates an MVar and the same three functions are evaluated in the
>>>> same order again.
>>>>
>>>> If the MVar is full, it creates another MVar of the same type and puts
>>>> it in the TChan.
>>>>
>>>> Is this a sound design? Does it prompt any questions from you? Here's
>>>> my question. If this is basically a sound design, I know I will need
>>>> use forkIO. I'm not sure where.
>>>> If this is not a sound design, please ask questions or give other
>>>> feedback so I can make changes and restore sanity.
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> [email protected]
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
------------------------------
Message: 4
Date: Wed, 7 Sep 2011 20:15:55 -0700
From: Michael Litchard <[email protected]>
Subject: Re: [Haskell-beginners] Help me with designing my daemon,
please.
To: David McBride <[email protected]>
Cc: [email protected]
Message-ID:
<caezekyp3zfey1bmvyzz8pb4rjhr9bxjyn6ppoyv0gtq4rcq...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
I think I've come up with something that's more simple and makes more
sense. I will probably want to run it by the list for a sanity check.
I'll update this thread when it's ready to display.
On Wed, Sep 7, 2011 at 3:21 PM, Michael Litchard <[email protected]> wrote:
> I need to provide more detail. Keep in mind that what I want to do,
> and the way I'm doing it may not fit at all. This code is using Chans
> and MVars, but could be using TChans and TVars easily enough. I kept
> things the way they are to just express what I'm trying to do
>
>> data ServerModel = PRODUCT1
>> ? ? ? ? ? ? ? ? ?| PRODUCT2
>> ? ? ? ? ? ? ? ? ?| PRODUCT3
>> ? ? ? ? ? ? ? ? ?| PRODUCT4
>> ? ? ? ? ? ? ? ? ?| PRODUCT5
>> ? ? ? ? ? ? ? ? ? ? ?deriving Show
>
>> newtype ProcessStep = PStep (ServerModel -> FilePath -> IO ())
>
>> type Cookie = String
>
>> type ProcessState = MVar ([ProcessStep],ProcessConfig)
>
>> data ProcessConfig = PConfig { model :: ServerModel
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?, ipAddress :: String
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?, rootDirectory :: FilePath
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?, cookie :: Cookie
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?} deriving Show
>
>> preProcess :: ServerModel -> FilePath -> IO ()
>> preProcess sModel fPath = putStrLn ("preProcessing" ++ show sModel)
>
>> initiatedJob :: ServerModel -> FilePath -> IO ()
>> initiatedJob sModel fPath = putStrLn ("in progress" ++ show sModel)
>
>> makeChart :: ServerModel -> FilePath -> IO ()
>> makeChart sModel fPath = putStrLn ("chart making" ++ show sModel)
>
> ?main :: IO ()
> ?main = do
> ? ? pState <- make
> ? ? world <- newEmptyMVar :: IO (ProcessState)
> ? ? worldQueue <- newChan :: IO (Chan ProcessState)
> ? ? installHandler userDefinedSignal2 (Catch $ worldHandler world worldQueue)
> N
> othing
> ? ? installHandler userDefinedSignal1 (Catch $ emptyQueue worldQueue
> ) Nothing ? ?---- This is for testing purposes. I just want to be able
> to empty the Chan and see if expected behavior holds
> ? ? installHandler nullSignal (Catch $ emptyMVar world ) Nothing
> ---- For testing as well, same reason as above.
> ? ? sequence_ $ repeat $ queueCheck
>
>
>> worldHandler :: ProcessState -> Chan ProcessState -> IO ()
>> worldHandler world worldQueue = do
>>
>> ? ? mvarState <- isEmptyMVar world
>> ? ? let tStep = PStep undefined
>> ? ? let tConfig = PConfig { model = undefined,
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ipAddress = undefined,
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? rootDirectory = undefined,
>> ? ? ? ? ? ? ? ? ? ? ? ? ? ? cookie = undefined
>> ? ? ? ? ? ? ? ? ? ? ? ? ? }
>>
>> ? ? let tState = undefined
>> ? ? case (mvarState) of
>> ? ? ? True ?-> putMVar world tState
>> ? ? ? False -> growQueue
>> ? ? ? ? where growQueue = do
>> ? ? ? ? ? ? ? ? ? newWorld <- newMVar ([tStep], tConfig)
>> ? ? ? ? ? ? ? ? ? writeChan worldQueue newWorld
>
>
>
> On Wed, Sep 7, 2011 at 1:37 PM, David McBride <[email protected]> wrote:
>> I'm imagining this:
>>
>> data TestInfo = {
>> ?testname :: String,
>> ?etc..
>> }
>> data TestResult = {
>> ?success :: Bool,
>> ?etc...
>> }
>>
>> data Test = Test (TestInfo -> IO ())
>> type Tests = [Test]
>>
>> main = do
>> ?let tests = [whatever] :: Tests
>> ?testchan <- newTChanIO :: IO (TChan TestInfo)
>> ?resultchan <- newTChanIO :: IO (TChan TestResult)
>> ?exceptionwhatever $ queuetest testchan
>> ?forkIO $ testThread (testchan,resultchan) tests
>> ?printTestResults resultChan
>>
>> queuetest chan = atomically $ writeTchan (TestInfo .....)
>>
>> testThread (testchan, resultchan) tests = forever $ do
>> ?newtest <- atomically $ readTChan testchan
>> ?results <- mapM tests newTest
>> ?atomically $ writeTChan resultchan results
>>
>> printTestResults chan = forever $ do
>> ?x <- atomically $ readTChan chan
>> ?print x
>>
>> Something like that perhaps?
>>
>>
>> On Wed, Sep 7, 2011 at 3:31 PM, Michael Litchard <[email protected]> wrote:
>>> This is what I am trying to do.
>>> I have tests to run and manage. I'm only running one test at a time.
>>> When my daemon gets a signal, it will either prep a test and run it,
>>> or queue the request. After it runs the test, I want it to check the
>>> queue for other tests that may have been requested.
>>> This is my first expedition into this domain. I'm trying to collect
>>> MVars and putting tem in a TChan is the way that seemed right, but I'm
>>> not sure at all. This is my first guess.
>>> I thought I needed a forked thread for the eventuality that I get a
>>> signal while my transaction is being executed.
>>> Have I clarified or further obfuscated?
>>>
>>> On Wed, Sep 7, 2011 at 12:22 PM, David McBride <[email protected]> wrote:
>>>> It sounds bizarre. ?Why pass around an mvar in tchan, when you could
>>>> just pass a maybe around and pattern match to see if it is Nothing or
>>>> not? ?Also, why have forkio and tchan at all if they are only going to
>>>> operate in sequence, one at a time?
>>>>
>>>> What exactly are you trying to do?
>>>>
>>>> On Wed, Sep 7, 2011 at 3:01 PM, Michael Litchard <[email protected]>
>>>> wrote:
>>>>> I have a daemon I need to build, and need to work out some design
>>>>> details I am having difficulty with. Here's what the design looks like
>>>>> right now
>>>>>
>>>>> When the daemon starts it creates an empty MVar and an empty TChan.
>>>>> Then it listens for a usrSIG1.
>>>>> when it gets one, it checks to see if the MVar is empty. If it is, it
>>>>> does some stuff to fill the MVar, which is then used to pass around
>>>>> state for a list of functions. These functions are always the same.
>>>>> After evaluating these functions, the TChan is checked. As long as the
>>>>> TChan has something in it,
>>>>> it populates an MVar and the same three functions are evaluated in the
>>>>> same order again.
>>>>>
>>>>> If the MVar is full, it creates another MVar of the same type and puts
>>>>> it in the TChan.
>>>>>
>>>>> Is this a sound design? Does it prompt any questions from you? Here's
>>>>> my question. If this is basically a sound design, I know I will need
>>>>> use forkIO. I'm not sure where.
>>>>> If this is not a sound design, please ask questions or give other
>>>>> feedback so I can make changes and restore sanity.
>>>>>
>>>>> _______________________________________________
>>>>> Beginners mailing list
>>>>> [email protected]
>>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>>
>>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>
------------------------------
Message: 5
Date: Thu, 8 Sep 2011 13:56:14 +0800
From: Zhi-Qiang Lei <[email protected]>
Subject: [Haskell-beginners] Question about lazy evaluation
To: Haskell Beginer <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hi,
I am confused about this piece of explanation in Real World Haskell.
-- file: ch08/append.hs
(++) :: [a] -> [a] -> [a]
(x:xs) ++ ys = x : (xs ++ ys)
[] ++ ys = ys
In a strict language, if we evaluate "foo" ++ "bar", the entire list is
constructed, then returned. Non-strict evaluation defers much of the work until
it is needed.
If we demand an element of the expression "foo" ++ "bar", the first pattern of
the function's definition matches, and we return the expression x : (xs ++ ys).
Because the (:) constructor is non-strict, the evaluation of xs ++ ys can be
deferred: we generate more elements of the result at whatever rate they are
demanded. When we generate more of the result, we will no longer be using x, so
the garbage collector can reclaim it. Since we generate elements of the result
on demand, and do not hold onto parts that we are done with, the compiler can
evaluate our code in constant space.
When ('f' : "oo") ++ "bar" becomes 'f' : ("oo" ++ "bar") and then becomes 'f' :
('o' : ("o" ++ "bar")), we still need 'f', don't we?
Best regards,
Zhi-Qiang Lei
[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110908/25cfece1/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 39, Issue 7
****************************************