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:  Foreign function interface? (aditya siram)
   2. Re:  edit-dist (Brent Yorgey)
   3.  Help using Type Families (MAN)
   4. Re:  Help using Type Families (Antoine Latter)
   5.  doc on accessing C struct binary data (Sean Perry)
   6.  Re: Announcement: Real World Haskell - Reading   Group
      (Benjamin L. Russell)
   7. Re:  doc on accessing C struct binary data (Stephen Tetley)


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

Message: 1
Date: Fri, 30 Jul 2010 15:49:06 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Foreign function interface?
To: [email protected]
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

>  Can it interface with C++ as well as C?
No you have to wrap your C++ code in C.
-deech <[email protected]>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100730/9e4dfd16/attachment-0001.html

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

Message: 2
Date: Fri, 30 Jul 2010 22:01:26 +0100
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] edit-dist
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Jul 30, 2010 at 04:45:02PM -0400, Andy Larocque wrote:
> 
> -- but when I replace 'best' with 'concat' in the transform
> -- function to see all the possibilities, some strange solutions appear.

That's because replacing 'best' with 'concat' doesn't do what you want
at all.  It sticks together three edit sequences into one long list,
which now does not really correspond to anything -- but notice that
transform makes recursive calls to itself, so now the results returned
by these recursive calls will be meaningless and all bets are off.

If you want to see all solutions returned, you can modify the
transform function like this:

    transform :: String -> String -> [[Edit]]
    transform [ ] [ ] =  [[]]
    transform xs [ ]  =  [[Kill]]
    transform [ ] ys  =  [map Insert ys]
    transform (x:xs) (y:ys)
      | x==y         =  map (Copy :) $ transform xs ys
      | otherwise    =  concat [ map (Delete :) $ transform xs (y:ys) ,
                                 map (Insert y :) $ transform (x:xs) ys,
                                 map (Change y :) $ transform xs ys ]
    
Note that now 'transform' returns a list of edit sequences instead of
just one, and then we have to make a few more adjustments, namely
wrapping the first few results in a singleton list, and adding calls
to 'map', since the recursive calls will return a list of solutions
and we want to (say) add 'Delete' to the beginning of each.

This gives me something sensible for your example:

    *Main> transform "AZ" "5Z"
    [[Delete,Delete,Insert '5',Insert 'Z'],
     [Delete,Insert '5',Copy],
     [Delete,Change '5',Insert 'Z'],
     [Insert '5',Delete,Copy],
     [Insert '5',Insert 'Z',Kill],
     [Insert '5',Change 'Z',Kill],
     [Change '5',Copy]]

-Brent


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

Message: 3
Date: Fri, 30 Jul 2010 19:06:49 -0300
From: MAN <[email protected]>
Subject: [Haskell-beginners] Help using Type Families
To: [email protected]
Message-ID: <1280527609.3154.17.ca...@dy-book>
Content-Type: text/plain; charset="UTF-8"

I desperately need help with this little predicament I got myself into.
I thought It'd be nice to write a piece of code that would wrap around
an older function, which given a bunch of parameters produces a plot (a
file) in the working directory. This new code receives the parameters
first, and only if the plot is not already present in the directory,
should pass the parameters to (and run) the wrapped function.

With that in mind I've defined the following type class. The older
function is treated as an anonymous 'muscle', capable of taking its
parameters ordered in some structure unknown.

> class Show parType => MuscleCapable parType where
>   -- | This could be a list or a tuple, etc. Whatever way of
implementing it, it
>   --   contains all necessary parameters to make the muscle produce a
resource.
>   --   Notice that this definition forces the type of ALL the
parameters of
>   --   the wrapped up muscle to be the same.
>   data Parameters parType
>
>   -- | A way to obtain the values of a @Parameters a@, for printing
purposes
>   listOfParams :: Parameters parType -> [parType]
>
>   -- | This wrapper around the muscle function will allow me to
produce a
>   --   resource on demand.
>   muscle :: Parameters parType -> IO FilePath

But when trying to use that, I get different errors at compile time
related to my (mis)use of type families, most of which are above my
head.
I use this dummy code to test the mechanism:

> data ThreeTuple a = T3 a a a
>
> listFromThreeTuple :: ThreeTuple a -> [a]
> listFromThreeTuple (T3 x y z) = [x,y,z]
>
> fun (T3 x y z) = do
>   withFile "output.out" WriteMode $ flip hPutStrLn (concatMap show
[x,y,z])
>   return "output.out"
>
> instance MuscleCapable Int where
>   data Parameters Int = ThreeTuple Int
>   listOfParams = listFromThreeTuple
>   muscle = fun

This fires the following error in GHC (I do add the TypeFamilies
LANGUAGE pragma):
"""
    Couldn't match expected type `Parameters Int'
           against inferred type `ThreeTuple Int'
      NB: `Parameters' is a type function
"""

If I use a similar data type without type parameters:

> data ThreeTuple = T3 Int Int Int

And modify the above snippet accordingly, I get the same thing:
"""
    Couldn't match expected type `Parameters Int'
           against inferred type `ThreeTuple'
      NB: `Parameters' is a type function
    In the expression: listFromThreeTuple
    In the definition of `listOfParams':
"""

Whatever alternative I try (and I have gone far along the trial-error
path) inevitably yields those errors for both lines implementing
'listOfParams' and 'muscle'.

Could somebody help me understand what's going on? I'm really lost at
this point.


PS: My first attempt at the class used 'type' to define the associated
type. I had to switch to 'data' due to problems regarding the chosen
type not being _injective_. GHC error messages and the paper "Fun with
type functions" helped on that.



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

Message: 4
Date: Fri, 30 Jul 2010 22:05:21 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Help using Type Families
To: MAN <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, Jul 30, 2010 at 5:06 PM, MAN <[email protected]> wrote:
> I desperately need help with this little predicament I got myself into.
> I thought It'd be nice to write a piece of code that would wrap around
> an older function, which given a bunch of parameters produces a plot (a
> file) in the working directory. This new code receives the parameters
> first, and only if the plot is not already present in the directory,
> should pass the parameters to (and run) the wrapped function.
>
> With that in mind I've defined the following type class. The older
> function is treated as an anonymous 'muscle', capable of taking its
> parameters ordered in some structure unknown.
>
>> class Show parType => MuscleCapable parType where
>>   -- | This could be a list or a tuple, etc. Whatever way of
> implementing it, it
>>   --   contains all necessary parameters to make the muscle produce a
> resource.
>>   --   Notice that this definition forces the type of ALL the
> parameters of
>>   --   the wrapped up muscle to be the same.
>>   data Parameters parType
>>
>>   -- | A way to obtain the values of a @Parameters a@, for printing
> purposes
>>   listOfParams :: Parameters parType -> [parType]
>>
>>   -- | This wrapper around the muscle function will allow me to
> produce a
>>   --   resource on demand.
>>   muscle :: Parameters parType -> IO FilePath
>
> But when trying to use that, I get different errors at compile time
> related to my (mis)use of type families, most of which are above my
> head.
> I use this dummy code to test the mechanism:
>
>> data ThreeTuple a = T3 a a a
>>
>> listFromThreeTuple :: ThreeTuple a -> [a]
>> listFromThreeTuple (T3 x y z) = [x,y,z]
>>
>> fun (T3 x y z) = do
>>   withFile "output.out" WriteMode $ flip hPutStrLn (concatMap show
> [x,y,z])
>>   return "output.out"
>>
>> instance MuscleCapable Int where
>>   data Parameters Int = ThreeTuple Int
>>   listOfParams = listFromThreeTuple
>>   muscle = fun
>
> This fires the following error in GHC (I do add the TypeFamilies
> LANGUAGE pragma):
> """
>    Couldn't match expected type `Parameters Int'
>           against inferred type `ThreeTuple Int'
>      NB: `Parameters' is a type function
> """
>
> If I use a similar data type without type parameters:
>
>> data ThreeTuple = T3 Int Int Int
>
> And modify the above snippet accordingly, I get the same thing:
> """
>    Couldn't match expected type `Parameters Int'
>           against inferred type `ThreeTuple'
>      NB: `Parameters' is a type function
>    In the expression: listFromThreeTuple
>    In the definition of `listOfParams':
> """
>
> Whatever alternative I try (and I have gone far along the trial-error
> path) inevitably yields those errors for both lines implementing
> 'listOfParams' and 'muscle'.
>
> Could somebody help me understand what's going on? I'm really lost at
> this point.
>
>
> PS: My first attempt at the class used 'type' to define the associated
> type. I had to switch to 'data' due to problems regarding the chosen
> type not being _injective_. GHC error messages and the paper "Fun with
> type functions" helped on that.
>

The error is with your data instance declaration:

> data Parameters Int = ThreeTuple Int

You're treating a data declaration as if it were a type declaration -
a data declaration needs to have a constructor, something like:

> data Paramter Int = ParamInt (ThreeTuple Int)

would be more likely to type check.

Then you'd have to pattern match on the constructor to deconstruct it.


Type errors aside, all of this looks really really complicated, and
from what you're describing I'm not sure it needs to be.

Why not just have two functions, one which wraps around the first? Or
if you want to write code in a reusable manner, ordinary higher-order
functions work well:

> writeToFileIfNonExistent :: FilePath -> (FilePath -> IO ()) -> IO ()
> writeToFileIfNonExistent path action
>  = do
>   exists <- doesFileExist path
>   unless exists $ action path
>   return ()

> coreFileWritingFunction :: Param1 -> Param2 -> Param3 -> FilePath -> IO ()
> coreFileWritingFunction p1 p2 p3 path = withFile path WriteMode $ \handle -> 
> ...

> slickNewWrapper :: Param1 -> Param2 -> Param3 -> FilePath -> IO ()
> slickNewWrapper p1 p2 p3 = writeToFileIfNonExistent $ coreFileWritingFunction 
> p1 p2 p3

I've chosen silly names for my functions, but I hope you get the idea.
Don't hesitate to respond if you have any questions.

Antoine


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

Message: 5
Date: Fri, 30 Jul 2010 23:40:47 -0700
From: Sean Perry <[email protected]>
Subject: [Haskell-beginners] doc on accessing C struct binary data
To: [email protected]
Message-ID: <1280558447.1375.6.ca...@aspire>
Content-Type: text/plain; charset="UTF-8"

I would like to read C structs that have been written to disk as binary
data. Is there a reference on doing this somewhere. The IO is not too
hard, but how do I mimic the C struct in Haskell and still honor the
exact sizes of the various struct members?




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

Message: 6
Date: Sat, 31 Jul 2010 16:12:14 +0900
From: [email protected] (Benjamin L. Russell)
Subject: [Haskell-beginners] Re: Announcement: Real World Haskell -
        Reading Group
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Jeff Greer <[email protected]> writes:

> We are organizing a reading group for O&#39;Reilly&#39;s - Real World 
> HaskellWe will start with Chapter 1 on August 8, 2010 and then cover 
> additional chapters at two week intervals.If interested: http://linuxagora.com
> This is an all volunteer web site - no advertising, no feesThank
> you,Jeff-- http://linuxagora.com

This is a great idea!  Here is a link to the sub-forum for the _Real
World Haskell_ reading group:

Real World Haskell - Introduction - Linux Agora Forums
http://www.linuxagora.com/vbforum/showthread.php?s=8c990bc26f3825f73a695273481dc42e&t=1207

I was surprised to find Japanese and Spanish translations of Justin
Bailey's Haskell cheatsheet [1].

Have you considered creating a mailing list gateway for the Web forum?
Most users here prefer a mailing list or newsgroup interface to that of
a Web forum.  Ideally, you could set up a mailing list first, and then
have the mailing list mirrored on a Web site.  For example, this mailing
list is mirrored on Gmane at the following site:

The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell ()
http://blog.gmane.org/gmane.comp.lang.haskell.beginners

-- Benjamin L. Russell

[1] Bailey, Justin. "The Haskell Cheatsheet." _CODESLOWER.COM,_
n.d. Web. 31 Jul. 2010. <http://cheatsheet.codeslower.com/>.
-- 
Benjamin L. Russell  /   DekuDekuplex at Yahoo dot com
http://dekudekuplex.wordpress.com/
Translator/Interpreter / Mobile:  +011 81 80-3603-6725
"Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ 



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

Message: 7
Date: Sat, 31 Jul 2010 09:11:50 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] doc on accessing C struct binary data
To: Sean Perry <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Sean

Commonly people would use Data.Word and Data.Int to get sized
integrals. There's no corresponding sized types for floats - if you're
lucky your serialized C Structs won't use floats otherwise you'll have
to dig out a reference manual to see how they are laid out.

To actually read C-structs Data.Binary.Get should provide the
primitives you need (getWord8, getWord16le, getWord16be, ...), you'll
then have to assemble a parser using these primitives to read your
struct.

You might have to pay some attention to alignment - the C struct might
be laid out with elements on byte boundaries (usually 4-byte) rather
than directly adjacent. I suspect alignment is compiler dependent, its
a long time since I looked at this but I believe C99 has pragmas to
direct the compiler on alignment.

Best wishes

Stephen


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

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


End of Beginners Digest, Vol 25, Issue 60
*****************************************

Reply via email to