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.  Printing the bits of an Int|Double (M Xyz)
   2. Re:  Is there a function to determine if a date is        a business
      day? (Deniz Dogan)
   3. Re:  Is there a function to determine if a date   isa business
      day? (Gaius Hammond)
   4. RE:  Is there a function to determine if a dateisa        business
      day? (Bayley, Alistair)
   5. Re:  Printing the bits of an Int|Double (Stephen Tetley)
   6. Re:  Printing the bits of an Int|Double (Stephen Tetley)
   7. RE:  how to use regex replace (Kui Ma)
   8.  How to create a monad instance (Marco De Oliveira)


----------------------------------------------------------------------

Message: 1
Date: Thu, 10 Dec 2009 23:14:06 -0800 (PST)
From: M Xyz <[email protected]>
Subject: [Haskell-beginners] Printing the bits of an Int|Double
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"


I've been trying to play around with binary data, but I haven't made much 
progress
trying to print the bits of a Double. With help from #haskell I've made it this 
far:

-- Printing the bits of an Int
main = do putStrLn $ showIntAtBase 2 (chr . (48+)) z ""

-- 103 = 1100111, after bit shifting 11001
z = shiftR (103 :: Int64) 2 

This is as far as I got with Doubles:

import Data.Binary.IEEE754
import qualified Data.ByteString.Lazy as BS
main = do BS.putStrLn $ runPut $ putFloat64be 4.123

Instead of playing with ByteStrings, is there just a way to fill an Int64 with 
the bits of a Double (Similar to Java's long = Double.doubleToLongBits(double))?



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091211/4f107c25/attachment-0001.html

------------------------------

Message: 2
Date: Fri, 11 Dec 2009 10:35:14 +0100
From: Deniz Dogan <[email protected]>
Subject: Re: [Haskell-beginners] Is there a function to determine if a
        date is a business day?
To: Hong Yang <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

2009/12/11 Hong Yang <[email protected]>:
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>

I don't know, but wouldn't it be nice if there was some package
dealing with "cultures"? Something along the lines of:

data Culture = Culture { country :: Country,
                         language :: Language,
                         isHoliday :: (Day -> Bool),
                         firstDayOfWeek :: Day,
                         ...
                       }

-- 
Deniz Dogan


------------------------------

Message: 3
Date: Fri, 11 Dec 2009 09:42:20 +0000
From: "Gaius Hammond" <[email protected]>
Subject: Re: [Haskell-beginners] Is there a function to determine if a
        date    isa business day?
To: "Deniz Dogan" <[email protected]>,
        [email protected], "Hong Yang" <[email protected]>
Cc: [email protected]
Message-ID:
        
<595676302-1260524619-cardhu_decombobulator_blackberry.rim.net-16187518...@bda221.bisx.produk.on.blackberry>
        
Content-Type: text/plain

You can buy this data from companies like Reuters. They collate it by having 
someone phone every embassy every year.


Cheers,



G



------Original Message------
From: Deniz Dogan
Sender: [email protected]
To: Hong Yang
Cc: [email protected]
Subject: Re: [Haskell-beginners] Is there a function to determine if a date isa 
business day?
Sent: 11 Dec 2009 09:35

2009/12/11 Hong Yang <[email protected]>:
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>

I don't know, but wouldn't it be nice if there was some package
dealing with "cultures"? Something along the lines of:

data Culture = Culture { country :: Country,
                         language :: Language,
                         isHoliday :: (Day -> Bool),
                         firstDayOfWeek :: Day,
                         ...
                       }

-- 
Deniz Dogan
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------

------------------------------

Message: 4
Date: Fri, 11 Dec 2009 09:47:01 -0000
From: "Bayley, Alistair" <[email protected]>
Subject: RE: [Haskell-beginners] Is there a function to determine if a
        dateisa business day?
To: <[email protected]>, "Hong Yang" <[email protected]>
Cc: [email protected]
Message-ID:
        <125eacd0cae4d24abdb4d148c4593da911026...@gblonxmb02.corp.amvescap.net>
        
Content-Type: text/plain; charset="us-ascii"

> From: [email protected] 
> [mailto:[email protected]] On Behalf Of Gaius Hammond
> 
> You can buy this data from companies like Reuters. They 
> collate it by having someone phone every embassy every year.

Or:
http://en.wikipedia.org/wiki/List_of_holidays_by_country

Alistair
*****************************************************************
Confidentiality Note: The information contained in this message,
and any attachments, may contain confidential and/or privileged
material. It is intended solely for the person(s) or entity to
which it is addressed. Any review, retransmission, dissemination,
or taking of any action in reliance upon this information by
persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the
sender and delete the material from any computer.
*****************************************************************



------------------------------

Message: 5
Date: Fri, 11 Dec 2009 14:05:07 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Printing the bits of an Int|Double
To: M Xyz <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello M Xyz

Here's a module[1] that seems to work for single precision floats -
you might want to do some checking of random numbers with toAndFro
(there's plenty of room for errors).

The print functions is printBin uses showIntAtBase which doesn't pad
with leading zeros, you might want to add them.


Best wishes

Stephen

[1] I wrote it a while ago but checking this morning it had some
nefarious bugs in.

module PrintIeeeFloat where

import Data.Bits
import Data.Char
import Data.Word
import Numeric


const_B :: Int
const_B = 127


printBin :: (Fractional a,Ord a) => a -> ShowS
printBin a =
    f s . showChar ' ' . f t . showChar ' ' . f u . showChar ' ' . f v
  where
    f = showIntAtBase 2 (chr . (48+))
    (s,t,u,v) = packIEEESingle a

toAndFro :: (Fractional a, Ord a) => a -> a
toAndFro a = let (s,t,u,v) = packIEEESingle a in unpackIEEESingle s t u v

unpackIEEESingle :: Fractional a => Word8 -> Word8 -> Word8 -> Word8 -> a
unpackIEEESingle b24_31 b16_23 b8_15 b0_7 =  sign $ fract * (2 ^^ expo)
  where
    sign  = if b24_31 `testBit` 7 then negate else id

    expo  = exponent' b24_31 b16_23

    fract = fraction b16_23 b8_15 b0_7


exponent' :: Word8  -> Word8 -> Int
exponent' a b = (a' `shiftL` 1) + (b' `shiftR` 7) - 127
  where
    a' = fromIntegral $ (a .&. 0x7f)
    b' = fromIntegral $ (b .&. 0x80)


fraction :: Fractional a => Word8 -> Word8 -> Word8 -> a
fraction b16_24 b8_15 b0_7 = 1.0 + ((fromIntegral frac) / (2 ^^ 23))
  where
   frac :: Int
   frac = (shiftL16 (b16_24 .&. 0x7f)) + (shiftL8 b8_15) + fromIntegral b0_7



packIEEESingle :: (Fractional a,Ord a) => a -> (Word8,Word8,Word8,Word8)
packIEEESingle a = (flipSign b24_31, exp_part+mant_part, b8_15, b0_7)
  where
    k     = findPosExpo $ abs a
    e     = k + const_B
    halfa = (abs a) / (2 ^^ fromIntegral k)
    f     = expand $ halfa - 1

    (b24_31, exp_part)          = expoWords e
    (mant_part,b8_15, b0_7)     = mantWords f

    flipSign = if a > 0 then id else (`setBit` 7)


findPosExpo :: (Fractional a, Ord a) => a -> Int
findPosExpo r | r <= 0    = 0
              | otherwise = step r 1
  where
    step r' k | r <= fromIntegral 2 ^^ k = k-1
              | otherwise                = step r' (k+1)


expand :: (Fractional a, Ord a) => a -> Word32
expand n = (`shiftR` 9) $ step n 0 id
  where
    step x ix f | x <= 0    = f (0::Word32)
                | otherwise = let y = 1 / (2 ^^ (ix+1))
                              in if x >= y
                                 then step (x-y) (ix+1) (f . (`setBit` (31-ix)))
                                 else step x (ix+1) f



-- 7 bits left, 1 bit right
expoWords :: Int -> (Word8,Word8)
expoWords n = (left, right)
  where
    right = if n `testBit` 0 then 128 else 0
    left  = fromIntegral $ n `shiftR` 1


mantWords :: Word32 -> (Word8,Word8,Word8)
mantWords x = (a,b,c)
  where
    c = fromIntegral $ x .&. 0xff
    b = fromIntegral $ (`shiftR` 8)  $ x .&. 0xff00
    a = fromIntegral $ (`shiftR` 16) $ x .&. 0xff0000

shiftL8 :: (Bits b, Integral b) => Word8 -> b
shiftL8 = (`shiftL` 8) . fromIntegral


shiftL16 :: (Bits b, Integral b) => Word8 -> b
shiftL16 = (`shiftL` 16) . fromIntegral


shiftL24 :: (Bits b, Integral b) => Word8 -> b
shiftL24 = (`shiftL` 24) . fromIntegral


w32be :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
w32be a b c d = (shiftL24 a) + (shiftL16 b) + (shiftL8 c) + fromIntegral d



2009/12/11 M Xyz <[email protected]>
>
> I've been trying to play around with binary data, but I haven't made much 
> progress
> trying to print the bits of a Double. With help from #haskell I've made it 
> this far:
>
> -- Printing the bits of an Int
> main = do putStrLn $ showIntAtBase 2 (chr . (48+)) z ""
>
> -- 103 = 1100111, after bit shifting 11001
> z = shiftR (103 :: Int64) 2
>
> This is as far as I got with Doubles:
>
> import Data.Binary.IEEE754
> import qualified Data.ByteString.Lazy as BS
> main = do BS.putStrLn $ runPut $ putFloat64be 4.123
>
> Instead of playing with ByteStrings, is there just a way to fill an Int64 
> with the bits of a Double (Similar to Java's long = 
> Double.doubleToLongBits(double))?
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


------------------------------

Message: 6
Date: Fri, 11 Dec 2009 14:29:06 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Printing the bits of an Int|Double
To: M Xyz <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Further...

If you need double precision you might have to do it yourself.

In absence of comments in the code I posted, the algorithm I used for
single precision is from here:

http://www.utdallas.edu/~cantrell/ee6481/lectures/float_comp.pdf

Eventually I'll get around to double precision, but not soon.

Best wishes

Stephen


------------------------------

Message: 7
Date: Fri, 11 Dec 2009 22:40:18 +0800
From: Kui Ma <[email protected]>
Subject: RE: [Haskell-beginners] how to use regex replace
To: <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="gb2312"


yes it's very simple but does satisfy my need. Thank you very much! 

 

actually I had spent time researching the regex packages on Hackage, it seems 
they doesn't provide such APIs for replace, but from my point of view the use 
of *replace* is as imporant as *match* in regular expression. Or I didn't find 
the right place?

 

Thanks again!
 
> Date: Thu, 10 Dec 2009 17:57:59 +0000
> From: [email protected]
> To: [email protected]
> CC: [email protected]
> Subject: Re: [Haskell-beginners] how to use regex replace
> 
> Hi,
> 
> Would Text.Regex.subRegex do? It is in package regex-compat. Usage is
> easy, see [1].
> 
> Sincerely,
> jan.
> 
> [1]: 
> http://hackage.haskell.org/packages/archive/regex-compat/0.92/doc/html/Text-Regex.html
> 
> On Thu, Dec 10, 2009 at 06:50:11PM +0800, Kui Ma wrote:
> > 
> > Hi,
> > 
> > Can anyone introduce me how to use regex replace or give me web page links 
> > about this topic? thank you in advance!
> > 
> > Regards
> > 
> 
> 
> -- 
> Heriot-Watt University is a Scottish charity
> registered under charity number SC000278.
> 
                                          
_________________________________________________________________
Keep your friends updated¡ªeven when you¡¯re not signed in.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091211/986a1119/attachment-0001.html

------------------------------

Message: 8
Date: Sat, 12 Dec 2009 01:17:38 +0100
From: Marco De Oliveira <[email protected]>
Subject: [Haskell-beginners] How to create a monad instance
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

Is it possible to create an instance of Monad for IOException?
Each time a try I stay blocked by IO monad.

with the definition:

data IOException a = IOExceptionCons (IO (Exception a))

data Exception a = SuccessCons (Maybe Warning) a
                          | ErrorCons Error

data Warning = WarningCons1
                    | WarningCons2

data Error = ErrorCons1
                | ErrorCons2

Regards


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 18, Issue 9
****************************************

Reply via email to