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: Some beginning questions for Haskell (Ertugrul Soeylemez)
2. Multidimensional Matrices in Haskell (Mihai Maruseac)
3. Re: Multidimensional Matrices in Haskell (Dave Bayer)
4. modules (Michael Mossey)
5. Re: modules (Michael Snoyman)
6. Re: Dynamic Programming in Haskell (Ali Razavi)
----------------------------------------------------------------------
Message: 1
Date: Wed, 7 Jul 2010 16:08:29 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: Some beginning questions for Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Liu shuping <[email protected]> wrote:
> I am new to Haskell, currently I am doing .Net development on windows
> platform. I began to know about Haskell for the reason when I knew
> some C# lambda features come from functional language. I am interested
> in Haskell from the very beginning when I saw it.
Hello there and welcome to the Haskell world. First of all, Haskell is
a general purpose language, so you can write any kind of problem. But
of course this is only half of the story.
> For I am really a very beginner, I get some basic questions:
> 1. Can you have some very general information to explain why use
> Haskell or functional language.
There are lots of pages on the WWW explaining "why functional
programming (FP) matters" (this is the name of one of them). To give a
short summary: FP gives you less programming errors, less code to type
and more readable code (to FP programmers, of course). Also modern FP
languages give you sophisticated type systems. Languages like C#, D and
Java approach this, but are far from comparable.
> 2. Is it a right choice of Haskell if I want to develop some geology
> software which mainly doing some huge numerical computation which
> takes long long time?
Probably yes. There are a few libraries for fast numerical
calculations, including bindings to well known number crunching
packages. If your favorite package is not among those, you can easily
make a binding on your own using the foreign function interface. This
is very easy.
Regarding performance: If you used to develop in C#, don't worry too
much. Your Haskell programs will probably be faster.
> 3. How about user interface, is Haskell capable to build application
> with complex user interface? or should I just use Haskell to build the
> core engine and user other language to build the user interface?
There are two major library bindings: Gtk2hs and wxHaskell. You can
use them to build any type of graphical user interface portably.
> 4. Is Haskell cross-platform? I mean if the Haskell source code is
> "code once and build everywhere?"
Much more than C#. In general, unless you use OS-specific functions,
you won't have any portability issues.
> 5. Are there any successful applications built with Haskell? they can
> give me a direct scene of what Haskell can do.
Nothing very big yet. Darcs is an excellent version control system.
GHC is the most mature Haskell compiler. Xmonad is a simple window
manager for X. There is also Frag, a small 3D shooter (essentially a
tech demo). Finally I would mention Happstack, a complete framework for
rapid and safe web development.
Now as I've noted above, this is only half of the story. The Haskell
user base is much smaller than the user bases of the most popular
languages, so there are less libraries and less applications.
One reason for this is that Haskell has a steep learning curve. Many
Haskell programmers find that in the first few days or even weeks they
do much more learning than actual programming. Not that they couldn't
get applications done, but Haskell is all about safety and correctness,
so you want to learn to do it properly instead of just doing it, like
you would in, say, PHP. This is why we say Haskell is valuable to know,
even if you don't use it.
Note that other languages are slowly adopting Haskell concepts. This
can be seen most clearly in Microsoft's F# language. It's nowhere near
Haskell, but you can see a lot of inspiration. It has very similar
syntax, algebraic types, currying and it even employs monadic
computations (they call them computation expressions).
Conclusion: Go ahead and learn Haskell. Then decide for yourself,
whether it's suitable for your project. Don't worry, learning Haskell
will always pay off, even if you don't use it. Also don't fear to use
languages, which are not widely used. Paul Graham has written a nice
anecdote about this [1]. You should definitely read it, it's a true
story.
[1] http://www.paulgraham.com/avg.html
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 2
Date: Wed, 7 Jul 2010 17:08:48 +0300
From: Mihai Maruseac <[email protected]>
Subject: [Haskell-beginners] Multidimensional Matrices in Haskell
To: [email protected], haskell <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
A friend of mine wanted to do some Cellular Automata experiments in
Haskell and was asking me what packages/libraries are there for
multidimensional matrices. I'm interested in both immutable and
mutable ones but I don't want them to be trapped inside a monad of any
kind.
Any hints?
--
MM
------------------------------
Message: 3
Date: Wed, 7 Jul 2010 07:56:47 -0700
From: Dave Bayer <[email protected]>
Subject: Re: [Haskell-beginners] Multidimensional Matrices in Haskell
To: Mihai Maruseac <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jul 7, 2010, at 7:08 AM, Mihai Maruseac wrote:
> I'm interested in both immutable and mutable ones but I don't want them to be
> trapped inside a monad of any kind.
If you want mutable data, you've imposing some serious constraints on the
sequence in which your program can be evaluated. Like death and taxes, you
cannot escape this. Monads are one way to handle this. If you want "plug and
play" they're probably the easiest way to handle this, for any other approach
you're going to have to get very involved in designing an alternative for
yourself.
By "trapped" do you mean that you don't want to write your whole program inside
"do" notation? By analogy, there are some sequence constraints in any program,
and some people would rather express as much of their program as possible using
function composition, rather than naming variables and passing arguments. This
is called "point-free" and if you like this, the pragmatic sweet spot is to
strive for it some but not all of the time.
By analogy, some of us find monads easiest to understand via Kleisli
composition, which is rather obvious, and we are baffled by the axioms used to
present monads in Haskell, which leave no impression whatsoever no matter how
many times one reads them.
At a pragmatic level, monads simply bury some plumbing under the street for
you. Very convenient. If you find yourself still writing everything using "do"
notation, it's because not everything was buried under the street for you.
Specialize the monad further to your setting, with a goal of expressing as much
as possible as Kleisli composition, introduce an infix composition operator for
this, and most of your code should end up composition, not "do" notation. This
is akin to striving for a point-free style in ordinary code.
I found this to be the case, writing parsers to transform text. Parsing is a
classic application of monads, but I was baffled why everyone thought the
parsers inside "do" notation where so pretty. I felt "trapped" as you fear
feeling. But because my return type was invariably ShowS, it was very easy to
write nearly everything as composition, regaining the clean feeling I remember
e.g. from similar code in Bigloo Scheme.
------------------------------
Message: 4
Date: Wed, 07 Jul 2010 09:18:47 -0700
From: Michael Mossey <[email protected]>
Subject: [Haskell-beginners] modules
To: Haskell-beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
So I created the following directory structure:
home /MusDoc/MusDoc.hs
home /MusDoc/FromXML.hs
home /MusDoc/Timing.hs
home /Midi/FromMusDoc.hs
I set -i to "home" referred to above, so I import these modules with
commands like
import MusDoc.MusDoc
import Midi.FromMusDoc
etc..
What do I do to import everything in MusDoc via
import MusDoc
?
I suspect this has something to do with packages... don't feel a need to
explain it all---point me to a good explanation and I'll probably figure it
out.
Mike
------------------------------
Message: 5
Date: Wed, 7 Jul 2010 19:38:35 +0300
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] modules
To: Michael Mossey <[email protected]>
Cc: Haskell-beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
On Wed, Jul 7, 2010 at 7:18 PM, Michael Mossey <[email protected]>wrote:
> So I created the following directory structure:
>
> home /MusDoc/MusDoc.hs
> home /MusDoc/FromXML.hs
> home /MusDoc/Timing.hs
> home /Midi/FromMusDoc.hs
>
> I set -i to "home" referred to above, so I import these modules with
> commands like
>
> import MusDoc.MusDoc
> import Midi.FromMusDoc
> etc..
>
> What do I do to import everything in MusDoc via
>
> import MusDoc
>
> ?
>
> I suspect this has something to do with packages... don't feel a need to
> explain it all---point me to a good explanation and I'll probably figure it
> out.
>
> Mike
>
>
> You'd need to create a file "MusDoc.hs" which imports all of the other
files, there's no way to automatically import all sub-modules (AFAIK).
Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100707/fccf9f56/attachment-0001.html
------------------------------
Message: 6
Date: Wed, 7 Jul 2010 12:40:07 -0400
From: Ali Razavi <[email protected]>
Subject: Re: [Haskell-beginners] Dynamic Programming in Haskell
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thanks a lot for your through and informative response. Your solution is
well-structured and pellucid. The only part I don't understand is in the
definition of Parens:
data Parens = Mul !Cost Dimension Parens Parens
| Matrix Dimension deriving (Show)
What is the exclamation mark in !Cost, and what does it signify?
Ali
> Date: Wed, 07 Jul 2010 11:30:28 +0200
> From: Heinrich Apfelmus <[email protected]>
> Subject: [Haskell-beginners] Re: Dynamic Programming in Haskell
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> Ali Razavi wrote:
> > In order to practice Haskell, I decided to program some algorithms from
> the
> > CLRS book. Particularly, I tried to implement the Matrix Chain Order from
> > Chapter 15 on Dynamic Programming.
> > Here is my code. It seems to work, however, it looks ugly and it was a
> > nightmare to debug. I appreciate comments about a more elegant solution,
> and
> > generally the best way to implement these kinds of algorithms in Haskell.
> > Style improvement suggestions are also welcome.
>
> Dynamic programming algorithms follow a common pattern:
>
> * Find a suitably small collection of subproblems that can be used to
> solve the original problem
> * Tabulate the solutions to the subproblems, also called *memoization*
>
> These are two separate concerns and, unlike the prototype imperative
> solutions, are best implemented separately.
>
> Thanks to lazy evaluation, memoization can be implemented very elegantly
> in Haskell. First, it should be a higher-order functions and second, you
> don't need to implement a particular order by which the memo table is
> filled, lazy evaluation will figure that out for you. You already know
> the latter trick, but here is another example:
>
> http://article.gmane.org/gmane.comp.lang.haskell.beginners/554
>
>
> But it doesn't stop here: there are very systemic ways to tackle the
> first part of dynamic programming, i.e. to *derive* dynamic programming
> algorithms from just the problem specification! An example and further
> references are given here
>
> http://thread.gmane.org/gmane.comp.lang.haskell.cafe/42316/focus=42320
>
>
> Concerning matrix chain multiplication, here is my implementation. Note
> the use of telling names and algebraic data types; there is no need to
> get lost in a maze of twisty little indexes, all alike.
>
> import Data.List
> import Data.Array
> import Data.Ord
>
> type Dimension = (Int,Int)
> type Cost = Int
> -- data type representing a parenthesization,
> -- caches cost to calculate and dimension of the result matrix
> data Parens = Mul !Cost Dimension Parens Parens
> | Matrix Dimension deriving (Show)
>
> -- retrieve cached vallues
> cost :: Parens -> Cost
> cost (Mul c _ _ _) = c
> cost (Matrix _) = 0
>
> dimension :: Parens -> Dimension
> dimension (Mul _ d _ _) = d
> dimension (Matrix d) = d
>
> -- smart constructor
> mul :: Parens -> Parens -> Parens
> mul x y = Mul (cost x + cost y + n*m*p) (n,p) x y
> where
> (n,m,p) = (fst $ dimension x, snd $ dimension x,
> snd $ dimension y)
>
> -- dynamic programming algorithm
> solve :: [Int] -> Parens
> solve matrices = chain 1 n
> where
> n = length matrices - 1
> dimensions = array (1,n) . zip [1..] $
> zip (init matrices) (tail matrices)
>
> chain = memoize n chain'
> chain' i j
> | i == j = Matrix (dimensions ! i)
> | otherwise = best [mul (chain i k) (chain (k+1) j)
> | k <- [i..j-1] ]
>
> best = minimumBy (comparing cost)
>
> -- memoize a function on a "square" [1..n] x [1..n]
> memoize :: Int -> (Int -> Int -> a) -> (Int -> Int -> a)
> memoize n f = \i j -> table ! (i,j)
> where
> table = array ((1,1),(n,n)) $
> [((i,j), f i j) | i <- [1..n], j <- [1..n]]
>
> Example output:
>
> *Main> cost $ solve [10,100,5,50,1]
> 1750
>
> I didn't need to debug this code, because it's obviously correct. Put
> differently, instead of spending my effort on debugging, I have spent it
> on making the solution elegant.
>
>
> Regards,
> Heinrich Apfelmus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100707/bb387340/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 25, Issue 23
*****************************************