Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  glib heterogenous hash table (John Obbele)
   2. Re:  glib heterogenous hash table (Felipe Lessa)
   3. Re:  Motivation to Learn Haskell (Greg)
   4.  containers and extensibility, typeclasses vs. multiple value
      constructors (Greg)
   5. Re:  containers and extensibility,        typeclasses vs. multiple
      value constructors (Daniel Fischer)
   6.  Re: Real world example of Typeclasses >  Interfaces
      (Ertugrul Soeylemez)
   7. Re:  containers and extensibility, typeclasses vs.        multiple
      value constructors (Stephen Tetley)


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

Message: 1
Date: Sun, 5 Sep 2010 22:57:45 +0200
From: John Obbele <john.obb...@gmail.com>
Subject: [Haskell-beginners] glib heterogenous hash table
To: beginners@haskell.org
Message-ID: <20100905205745.ga9...@megabook.localdomain>
Content-Type: text/plain; charset="utf-8"

Hi everyone !

To exercise my Haskell skills, I'm currently trying to bind
libxfconf-0 with Haskell. A small preview can be found on
patch-tag[0].

[0]: https://patch-tag.com/r/obbele/xfconf/home

Since XFCE projects rely heavily on glib, I am borrowing (lots
of) code from the gtk2hs package(s). I am still having issues
with gobject casts and foreign pointer finalizers, but lets focus
on GHashTable for now.

For those who are not familiar with glib (neither I am),
GHashTable[1] are hashes with homogenous key types but values of
arbitrary types. In xfconf, keys are all cstring but values can
be any type of gpointers (gchar *, gint*, gdouble*, widget*,
struct* ?).

[1]: http://library.gnome.org/devel/glib/stable/glib-Hash-Tables.html

I've tried to wrap the glib hash table in a haskell data type
this way:

> data GHashTable k v = GHashTable GHashTablePtr

The GHashTablePtr is our Haskell foreign pointer, courtesy
of C→HS. It is bound later with a g_hash_table_destroy finalizer
in order to (automatically) manage memory .

My real problems are:
- I cannot simply map (GHashTable k v) to a (Data.Map.Map k v)

- I don't know how to constraint k and v to be C pointers (Ptr
  CChar is ok but not Char or CChar).

Does anyone have a solution or some pointers on it ?

regards,
/John
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100905/eab824c9/attachment-0001.bin

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

Message: 2
Date: Sun, 5 Sep 2010 22:59:41 -0300
From: Felipe Lessa <felipe.le...@gmail.com>
Subject: Re: [Haskell-beginners] glib heterogenous hash table
To: beginners@haskell.org
Message-ID:
        <aanlkti=7hjqv2fzbhytu-g9uz-s-u7tpcjx7onm4x...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hello!

I don't know how many Gtk2Hs hackers lurk around the beginners list.
You should probably repost your question to Gtk2Hs' mailing list, see
[1].

[1] http://www.haskell.org/gtk2hs/documentation/#help

Cheers!

-- 
Felipe.


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

Message: 3
Date: Sun, 05 Sep 2010 21:16:27 -0700
From: Greg <gregli...@me.com>
Subject: Re: [Haskell-beginners] Motivation to Learn Haskell
To: beginners@haskell.org
Message-ID: <fe39b238-1ace-4749-9b36-7c909dd87...@me.com>
Content-Type: text/plain; charset=us-ascii

I think the financial industry is attracted to Haskell because it's lazy.  
(ba-dum dum!)

I'm in a similar spot as you are, and I'm making a second or third attempt to 
get a handle on Haskell (depending on how you count).  I'm doing it for much 
the same reasons you are, and for similar applications.

In my case, most of the simulation work is done in Matlab, and partially in C.  
C is undisciplined and error prone, which makes it slow to develop simulations 
in, and which is why people tend towards Matlab.  Matlab is much slower, and 
has the unfortunate tendency to treat everything in the world as though it were 
linear algebra-- but it's relatively rapid for development.

I've dropped my pursuit of Haskell a couple times after a few days each time 
for the reasons you're alluding to (and because the type system hurts my head), 
but I'm back at it again because it feels like there's something of value in 
it.  

In part, it feels more elegant-- in the same way that Scheme felt elegant (hey, 
don't judge me...).  In particular, lazy-functional seems like a really good 
match for simulation work: it feels right to represent the world as f(t) and to 
feed it time as an infinite list rather that write a loop that basically says 
"while the world hasn't ended, do:".  It also holds the promise of 
straight-forward parallelization which is critical for anything requiring 
performance on a modern platform.  

I haven't worked with R, so I can't comment on it, but for me Haskell is 
different enough from the other languages I work with that it forces me to 
(which I hope later means "allows me to") look at problems differently.  I 
think that's what you mean by a mind expanding exercise, and it's  a benefit I 
found in learning C++ after C, but not a benefit I found in learning Python 
after C++ because they're just too similar in concept.

There is quite a wealth of libraries available through the hackage system, but 
I'm choosing to treat the lack of established libraries in some areas as an 
opportunity-- I find writing algorithms I'm familiar with is a great way to get 
comfortable.  Once I know more of what I'm doing, I'll probably start pulling 
in more established packages just for their better optimized implementations 
and for compatibility reasons.

Cheers--
 Greg




On Sep 3, 2010, at 3:57 PM, Lorenzo Isella wrote:

> Dear All,
> It is my first post to this list and please do not take it as an attempt to 
> start any flamewar.
> From time to time, I try to find the motivation to learn at least the 
> fundamentals of another programming language.
> I normally use R and Python on a daily basis (but I am not that much into OO 
> programming) and have a good knowledge of Fortran and a rather superficial 
> one of C.
> Beside learning a new language as a sort of mind expanding exercise, I try to 
> figure out how and if it can save me some time in my work and how it measures 
> up against other languages.
> These days I tend to rely on R for data analysis and visualization whereas I 
> use Python (in particular Numpy+SciPy) for number crunching (it is very 
> convenient to use scipy/numpy to solve ODE's, manipulate arrays and so on).
> Now, I wonder what benefit I would gain from learning Haskell since I mainly 
> write codes for numerical simulations/data analysis.
> I know Haskell is gaining momentum e.g. in the financial environment (I 
> happened to see Haskell knowledge as a specification in some quant jobs) 
> hence it must be more than suitable for numerical work and, by the little I 
> have understood so far, it allows one to write code really resembling 
> mathematical expressions (I was impressed by guards and curried functions).
> However, it also looks to me (correct me if I am mistaken) that Haskell is a 
> far cry from the wealth of standard and contributed scientific modules you 
> have in Python or R and thanks to which you do not re-implement the wheel 
> yourself.
> Any thoughts/suggestions are really appreciated.
> Cheers
> 
> Lorenzo
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 4
Date: Sun, 05 Sep 2010 21:47:16 -0700
From: Greg <gregli...@me.com>
Subject: [Haskell-beginners] containers and extensibility, typeclasses
        vs. multiple value constructors
To: beginners@haskell.org
Message-ID: <2f44c0c5-2fd4-431e-b230-cce6e8fe7...@me.com>
Content-Type: text/plain; charset=us-ascii

I think I'm starting to get a sense for this, but want to check my 
understanding...

I'm writing a program that plots data.  I want to be able to have multiple 
plots simultaneously-- some are scatter plots, some are lines, some are 
functions, etc.  I think there are two choices in how to define these different 
plot styles: I can have one type with constructors for scatter, line, function, 
etc; or I can have independent types for each plot style and a typeclass to 
define the shared functionality.

I want to be able to treat these plots abstractly within the program: I don't 
care what style of plot it is at certain levels of processing, I just know I 
want to render it.  This suggests a typeclass handling, where the typeclass has 
a render method.

However, I also want to maintain a list of these plots so I can 'mapM_ render 
plotlist'.  I believe I can only create a list of plots if they're all the same 
type, therefore they must be multiple constructors for the same type.  'render' 
then will need to be handled by pattern matching.

The last piece of this, is that it would be nice to be able to add new plot 
styles later.  Ideally this will all become a library for me, and I'd like to 
be able to add a new plot style without having to modify the existing code.  I 
think the only way to do this is through the typeclass mechanism, where I can 
create the new type and create a class instance in my local code.

So, am I stuck with this tradeoff?  I can either put my plots in a container 
using one type and multiple constructors, or I can make the system extensible 
using typeclasses?

Thanks--
 Greg



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

Message: 5
Date: Mon, 6 Sep 2010 15:14:04 +0200
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] containers and extensibility,
        typeclasses vs. multiple value constructors
To: beginners@haskell.org
Message-ID: <201009061514.04718.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

On Monday 06 September 2010 06:47:16, Greg wrote:
> I think I'm starting to get a sense for this, but want to check my
> understanding...
>
> I'm writing a program that plots data.  I want to be able to have
> multiple plots simultaneously-- some are scatter plots, some are lines,
> some are functions, etc.  I think there are two choices in how to define
> these different plot styles: I can have one type with constructors for
> scatter, line, function, etc; or I can have independent types for each
> plot style and a typeclass to define the shared functionality.
>
> I want to be able to treat these plots abstractly within the program: I
> don't care what style of plot it is at certain levels of processing, I
> just know I want to render it.  This suggests a typeclass handling,
> where the typeclass has a render method.
>
> However, I also want to maintain a list of these plots so I can 'mapM_
> render plotlist'.  I believe I can only create a list of plots if
> they're all the same type,

Right.

> therefore they must be multiple constructors
> for the same type.  'render' then will need to be handled by pattern
> matching.

Weeeelll,

>
> The last piece of this, is that it would be nice to be able to add new
> plot styles later.  Ideally this will all become a library for me, and
> I'd like to be able to add a new plot style without having to modify the
> existing code.  I think the only way to do this is through the typeclass
> mechanism, where I can create the new type and create a class instance
> in my local code.
>
> So, am I stuck with this tradeoff?  I can either put my plots in a
> container using one type and multiple constructors, or I can make the
> system extensible using typeclasses?

you can combine the approaches. As long as all you want to do with your 
container is rendering the contents, 

{-# LANGUAGE ExistentialQuantification #-}

class Plot a where
    render :: a -> IO ()
    describe :: a -> String

data SomePlot = forall p. Plot p => SomePlot p

instance Plot SomePlot where
    render (SomePlot p) = render p
    describe (SomePlot p) = describe p

gives you a class, so you can define new plot types without modifying the 
library, and a wrapper type, so you can stick plots of different types in 
the same container after wrapping them.
Once they're wrapped, you can't use anything but the methods of the Plot 
class on them, though, so if you want to do anything else with the 
container's contents, that won't work (you can do something with an 
additional Typeable constraint).

http://www.haskell.org/haskellwiki/Existential_type for more.

>
> Thanks--
>  Greg
>

HTH,
Daniel



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

Message: 6
Date: Mon, 6 Sep 2010 16:47:21 +0200
From: Ertugrul Soeylemez <e...@ertes.de>
Subject: [Haskell-beginners] Re: Real world example of Typeclasses >
        Interfaces
To: beginners@haskell.org
Message-ID: <20100906164721.2375d...@tritium.streitmacht.eu>
Content-Type: text/plain; charset=US-ASCII

Alec Benzer <alecben...@gmail.com> wrote:

> I was speaking more generally, not specifically about the Haskell
> typeclasses Num and Enum (although actually irrational numbers aren't
> enumerable now that I think of it, but I also guess that's a
> relatively moot point since you can't really represent irrational
> numbers in a programming language)

There is a Num instance, which doesn't make sense to be an Enum
instance:

  instance Num a => Num (a -> a) where
    f + g = \x -> f x + g x
    -- ...

This makes some formulas look much nicer and more convenient to write:

  constOne :: Floating a => a -> a
  constOne = sin^2 + cos^2

Unfortunately Eq and Show are superclasses of Num, so you need a few
bogus instances:

  instance Eq (a -> a) where
    (==) = undefined

  instance Show (a -> a) where
    show = const "<function>"

As an alternative you can do this with the reader functor, too, although
it doesn't look nearly as nice for the above formula:

  constOne :: Floating a => a -> a
  constOne = (+) <$> (^2) . sin <*> (^2) . cos

It looks great for other formulas, though, and is more general:

  splits :: [a] -> [([a], [a])]
  splits = zip <$> inits <*> tails


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/




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

Message: 7
Date: Mon, 6 Sep 2010 15:57:07 +0100
From: Stephen Tetley <stephen.tet...@gmail.com>
Subject: Re: [Haskell-beginners] containers and extensibility,
        typeclasses vs. multiple value constructors
Cc: beginners@haskell.org
Message-ID:
        <aanlktinm0xk8mdlgwt8tqqyakfdzn1eguc6b+v5p8...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hello Greg

Supposing you are working with GnuPlot - one option is to make Plot a
functional type that takes a list of some polymorphic input data and
generates a 'GnuPlot' - where GnuPlot is a type representing output in
the GnuPlot format.

type Plot a = [a] -> GnuPlot

Or if GnuPlot accepts drawing styles...

type Plot a = [a] -> DrawingStyle -> GnuPlot

Plots are just functions, so clearly you can define as many as you
like and they are all the same type.

Best wishes

Stephen


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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 15
*****************************************

Reply via email to