[Haskell-cafe] searching haskell.org

2006-10-15 Thread Tamas K Papp
Hi,

I noticed that searching on Haskell.org (using the Search feature at
the bottom) doesn't work as I expected.  For example, searching for
memoise produces no results.

http://www.google.com/search?q=memoise+site%3Ahaskell.org produces 18
hits.  Is this intentional?

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Debugging Newton's method for square roots

2006-10-15 Thread Tamas K Papp
On Sun, Oct 15, 2006 at 12:17:20PM +, Vraj Mohan wrote:
 I am new to Haskell and need help in debugging my code.
 
 I wrote the following function for calculating square roots using Newton's 
 method:
 
 my_sqrt :: Float - Float
 my_sqrt x = improve 1 x
  where improve y x = if abs (y * y - x)  epsilon 
 then y 
 else improve ((y + (x/y))/ 2) x
epsilon = 0.1
 
 
 
 This works for several examples that I tried out but goes into an infinite 
 loop
 for my_sqrt 96. How do I go about debugging this code in GHC or Hugs?
 
 (The equivalent code is well-behaved on MIT Scheme)

Hi Vraj,

1. First, generally about debugging: I asked this question a month
ago, on this mailing list.  See this thread:

http://www.haskell.org/pipermail/haskell-cafe/2006-September/017858.html

2. Newton's method is not guaranteed to converge.  For examples, and a
nice introduction to uni- and multivariate rootfinding, see Numerical
Methods in Economics, Kenneth L Judd, Chapter 5.

I suggest that you use bisection, it is much more robust.  I have
written a bisection routine which I have been planning to post after a
bit of testing, maybe you will find it useful:

bisection tol reltol f a b | tol  0 || reltol  0 =
   error bisection needs positive tolerances
   | abs fa = tol = Just a -- a is a root
   | abs fb = tol = Just b -- b is a root
   | fa*fb  0 = Nothing -- IVT not guaranteed
   | a  b = bis b a fb fa -- exchange endpoints
   | otherwise = bis a b fa fb -- standard case
where
  fa = f a  -- function evaluated only once at each
  fb = f b  -- endpoint, store them here
  bis a b fa fb = let m = (a+b)/2
  fm = f m in
  if (b-a) = reltol*(1+abs(a)+abs(b)) || abs fm = tol
  then Just m
  -- implicit assumption: fa * fb  0
  else if fm*fa  0 -- new interval: [a,m]
   then bis a m fa fm
   else bis m b fm fb -- new interval: [m,b]

-- uniroot is a specialisation of bisection, with commonly used
-- tolerance values
uniroot = bisection 1e-20 1e-15


Untested code:

mysqrt x = uniroot f 0 x
   where f y = y**2-x

It will give you a Maybe a type value, ie Nothing for negative
numbers.  Comments about my code are of course welcome, I am a newbie
myself.

Best,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] searching haskell.org

2006-10-15 Thread Tamas K Papp
On Sun, Oct 15, 2006 at 03:41:53PM +0100, Neil Mitchell wrote:
 Hi
 
  I noticed that searching on Haskell.org (using the Search feature at
  the bottom) doesn't work as I expected.  For example, searching for
  memoise produces no results.
 
 This is searching haskellwiki.
 
 Does anyone have the search logs for this page? If they were
 available, we might get a better idea of what people were searching
 for, and could then make the results more appropriate. For example,
 its entirely possible people are searching for functions (better
 prodivided by Hoogle search), wiki concepts (as currently searched
 for), haskell stuff (a google site search), or possibly something
 entirely different.
 
 My experience from Hoogle suggests that people don't use your search
 like you think they do!

Typing haskell.org takes you to a page which has a search box at the
bottom, so if the user doesn't look at the browser address bar (which
does display http://haskell.org/haskellwiki/Haskell, indicating that
you are inside the wiki), it is reasonable to suppose that the search
box will search the whole site.

I think it would be good to have multiple buttons, eg Search
haskell.org (includes next two), Search HaskellWiki, Search
mailing lists.

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Yet Another Haskell Tutorial Question

2006-10-11 Thread Tamas K Papp
On Wed, Oct 11, 2006 at 09:16:57PM -0400, Bill Mill wrote:
 I've been working through the Yet Anther Haskell Tutorial at
 http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf , and I've gotten up
 to Excercise 4.6. It says:

Have you checked the solution?  Page 167.

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] question - which monad to use?

2006-10-02 Thread Tamas K Papp
Matthias,

Sorry if I was not clear in stating the problem.  Your solution works
nicely, but I would like to try writing a monad.  This is what I came
up with:

type Failure = String
data Computation a = Computation (Either Failure a) [a]

instance Monad Computation where
(Computation (Left e) h) = f = Computation (Left e) h -- do not proceed
(Computation (Right a) h) = f = let r = f a -- result
  h' = case r of
 Left e - h
 Right a' - a':h
  in
Computation r h'
return (s,c) = Computation (Right (s,c)) [(s,c)]

Basically, I want the = operator to call f on the last result, if it
is not a failure, and append the new result to the list (if it didn't
fail).

However, I am getting the following error message:

/home/tpapp/doc/research/pricespread/Main.hs:62:58:
Couldn't match the rigid variable `b' against the rigid variable `a'
  `b' is bound by the type signature for `='
  `a' is bound by the type signature for `='
  Expected type: [b]
  Inferred type: [a]
In the second argument of `Computation', namely `h'
In the definition of `=':
= (Computation (Left e) h) f = Computation (Left e) h

I don't know what the problem is.

Thanks,

Tamas

On Mon, Oct 02, 2006 at 03:54:23PM +0200, Matthias Fischmann wrote:

 hi, i don't fully understand your problem, but perhaps you could use
 iterate to produce a list or type [Result a], ie, of all computation
 steps, and then use this function to extract either result or error
 from the list:
 
 
 type Failmessage = Int
 data Result a = Root a | Failure Failmessage  deriving (Show)
 
 f :: [Result a] - Either a (Int, [Result a])
 f cs = f [] cs
 where
 f (Root r:_) [] = Left r
 f l [Failure i] = Right (i, reverse l)
 f l (x:xs)  = f (x:l) xs
 
 cs = [Root 1.2, Root 1.4, Root 1.38, Root 1.39121]
 cs' = [Root 1.2, Root 1.4, Root 1.38, Failure 1]
 
 -- f cs  == Left 1.39121
 -- f cs' == Right (1,[Root 1.2,Root 1.4,Root 1.38])
 
 
 (although this way you probably have the list still floating around
 somewhere if you process the error returned by f, so f should probably
 just drop the traversed part of the list.)
 
 hth,
 matthias
 
 
 
 On Sun, Oct 01, 2006 at 06:00:43PM -0400, Tamas K Papp wrote:
  To: Haskell Cafe haskell-cafe@haskell.org
  From: Tamas K Papp [EMAIL PROTECTED]
  Date: Sun, 1 Oct 2006 18:00:43 -0400
  Subject: [Haskell-cafe] question - which monad to use?
  
  Hi,
  
  I have a computation where a function is always applied to the
  previous result.  However, this function may not return a value (it
  involves finding a root numerically, and there may be no zero on the
  interval).  The whole problem has a parameter c0, and the function is
  also parametrized by the number of steps that have been taken
  previously.
  
  To make things concrete,
  
  type Failmessage = Int  -- this might be something more complex
  data Result a = Root a | Failure Failmessage -- guess I could use Either too
  
  f :: Double - Int - Double 0 - Result Double
  f c0 0 _ = c0
  f c0 j x = {- computation using x, parameters calculated from c0 and j -}
  
  Then
  
  c1 = f c0 0 c0
  c2 = f c0 1 c1
  c3 = f c0 2 c2
  ...
  
  up to cn.
  
  I would like to
  
  1) stop the computation when a Failure occurs, and store that failure
  
  2) keep track of intermediate results up to the point of failure, ie
  have a list [c1,c2,c3,...] at the end, which would go to cn in the
  ideal case of no failure.
  
  I think that a monad would be the cleanest way to do this.  I think I
  could try writing one (it would be a good exercise, I haven't written
  a monad before).  I would like to know if there is a predefined one
  which would work.
  
  Thank you,
  
  Tamas
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 -- 
 Institute of Information Systems, Humboldt-Universitaet zu Berlin
 
 web:  http://www.wiwi.hu-berlin.de/~fis/
 e-mail:   [EMAIL PROTECTED]
 tel:  +49 30 2093-5742
 fax:  +49 30 2093-5741
 office:   Spandauer Strasse 1, R.324, 10178 Berlin, Germany
 pgp:  AD67 CF64 7BB4 3B9A 6F25  0996 4D73 F1FD 8D32 9BAA



 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] question - which monad to use?

2006-10-02 Thread Tamas K Papp
On Mon, Oct 02, 2006 at 11:35:40AM -0400, Tamas K Papp wrote:
 Matthias,
 
 Sorry if I was not clear in stating the problem.  Your solution works
 nicely, but I would like to try writing a monad.  This is what I came
 up with:
 
 type Failure = String
 data Computation a = Computation (Either Failure a) [a]
 
 instance Monad Computation where
 (Computation (Left e) h) = f = Computation (Left e) h -- do not proceed
 (Computation (Right a) h) = f = let r = f a -- result
   h' = case r of
  Left e - h
  Right a' - a':h
   in
 Computation r h'
 return (s,c) = Computation (Right (s,c)) [(s,c)]

sorry, I pasted an older version.  This line should be

return a = Computation (Right a) [a]

 Basically, I want the = operator to call f on the last result, if it
 is not a failure, and append the new result to the list (if it didn't
 fail).
 
 However, I am getting the following error message:
 
 /home/tpapp/doc/research/pricespread/Main.hs:62:58:
 Couldn't match the rigid variable `b' against the rigid variable `a'
   `b' is bound by the type signature for `='
   `a' is bound by the type signature for `='
   Expected type: [b]
   Inferred type: [a]
 In the second argument of `Computation', namely `h'
 In the definition of `=':
   = (Computation (Left e) h) f = Computation (Left e) h
 
 I don't know what the problem is.
 
 Thanks,
 
 Tamas
 
 On Mon, Oct 02, 2006 at 03:54:23PM +0200, Matthias Fischmann wrote:
 
  hi, i don't fully understand your problem, but perhaps you could use
  iterate to produce a list or type [Result a], ie, of all computation
  steps, and then use this function to extract either result or error
  from the list:
  
  
  type Failmessage = Int
  data Result a = Root a | Failure Failmessage  deriving (Show)
  
  f :: [Result a] - Either a (Int, [Result a])
  f cs = f [] cs
  where
  f (Root r:_) [] = Left r
  f l [Failure i] = Right (i, reverse l)
  f l (x:xs)  = f (x:l) xs
  
  cs = [Root 1.2, Root 1.4, Root 1.38, Root 1.39121]
  cs' = [Root 1.2, Root 1.4, Root 1.38, Failure 1]
  
  -- f cs  == Left 1.39121
  -- f cs' == Right (1,[Root 1.2,Root 1.4,Root 1.38])
  
  
  (although this way you probably have the list still floating around
  somewhere if you process the error returned by f, so f should probably
  just drop the traversed part of the list.)
  
  hth,
  matthias
  
  
  
  On Sun, Oct 01, 2006 at 06:00:43PM -0400, Tamas K Papp wrote:
   To: Haskell Cafe haskell-cafe@haskell.org
   From: Tamas K Papp [EMAIL PROTECTED]
   Date: Sun, 1 Oct 2006 18:00:43 -0400
   Subject: [Haskell-cafe] question - which monad to use?
   
   Hi,
   
   I have a computation where a function is always applied to the
   previous result.  However, this function may not return a value (it
   involves finding a root numerically, and there may be no zero on the
   interval).  The whole problem has a parameter c0, and the function is
   also parametrized by the number of steps that have been taken
   previously.
   
   To make things concrete,
   
   type Failmessage = Int  -- this might be something more complex
   data Result a = Root a | Failure Failmessage -- guess I could use Either 
   too
   
   f :: Double - Int - Double 0 - Result Double
   f c0 0 _ = c0
   f c0 j x = {- computation using x, parameters calculated from c0 and j -}
   
   Then
   
   c1 = f c0 0 c0
   c2 = f c0 1 c1
   c3 = f c0 2 c2
   ...
   
   up to cn.
   
   I would like to
   
   1) stop the computation when a Failure occurs, and store that failure
   
   2) keep track of intermediate results up to the point of failure, ie
   have a list [c1,c2,c3,...] at the end, which would go to cn in the
   ideal case of no failure.
   
   I think that a monad would be the cleanest way to do this.  I think I
   could try writing one (it would be a good exercise, I haven't written
   a monad before).  I would like to know if there is a predefined one
   which would work.
   
   Thank you,
   
   Tamas
   ___
   Haskell-Cafe mailing list
   Haskell-Cafe@haskell.org
   http://www.haskell.org/mailman/listinfo/haskell-cafe
  
  -- 
  Institute of Information Systems, Humboldt-Universitaet zu Berlin
  
  web:  http://www.wiwi.hu-berlin.de/~fis/
  e-mail:   [EMAIL PROTECTED]
  tel:  +49 30 2093-5742
  fax:  +49 30 2093-5741
  office:   Spandauer Strasse 1, R.324, 10178 Berlin, Germany
  pgp:  AD67 CF64 7BB4 3B9A 6F25  0996 4D73 F1FD 8D32 9BAA
 
 
 
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http

[Haskell-cafe] cumulative sum

2006-10-02 Thread Tamas K Papp
Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]

2) the cumulative sum of p*lambda (multiplication elementwise)

Once I know how to do the first, I know how to do the second I guess
(doing the multiplication using zipWith to get the p*lambda list, but
I would be interested in any other suggestions).  Currently I take and
sum, but then I am calculating the same sums many times.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] question - which monad to use?

2006-10-01 Thread Tamas K Papp
Hi,

I have a computation where a function is always applied to the
previous result.  However, this function may not return a value (it
involves finding a root numerically, and there may be no zero on the
interval).  The whole problem has a parameter c0, and the function is
also parametrized by the number of steps that have been taken
previously.

To make things concrete,

type Failmessage = Int  -- this might be something more complex
data Result a = Root a | Failure Failmessage -- guess I could use Either too

f :: Double - Int - Double 0 - Result Double
f c0 0 _ = c0
f c0 j x = {- computation using x, parameters calculated from c0 and j -}

Then

c1 = f c0 0 c0
c2 = f c0 1 c1
c3 = f c0 2 c2
...

up to cn.

I would like to

1) stop the computation when a Failure occurs, and store that failure

2) keep track of intermediate results up to the point of failure, ie
have a list [c1,c2,c3,...] at the end, which would go to cn in the
ideal case of no failure.

I think that a monad would be the cleanest way to do this.  I think I
could try writing one (it would be a good exercise, I haven't written
a monad before).  I would like to know if there is a predefined one
which would work.

Thank you,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] smallest double eps

2006-09-30 Thread Tamas K Papp
On Sat, Sep 30, 2006 at 04:19:50AM -0400, Lennart Augustsson wrote:
 Hang on, hang on, now I'm getting confused.
 
 First you asked for the smallest (positive) x such that
1+x /= x
 which is around x=4.5e15.
 Then Joachim wondered if you wanted
1+x /= 1
 which is around x=2.2e-16.

Oops, sorry, there was a typo in my original post.  I was looking for
the latter.

Thanks for the help,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] smallest double eps

2006-09-29 Thread Tamas K Papp
Hi,

Is there a built-in constant in Haskell (or, if it is
compiler-specific, in ghc) that gives the smallest positive floating
point number x such that 1+x /= x?  Some languages refer to that as
double.eps or similar.  I need it for numeric algorithms.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] smallest double eps

2006-09-29 Thread Tamas K Papp
On Sat, Sep 30, 2006 at 12:20:16AM +, Joachim Breitner wrote:
 Hi,
 
 Am Freitag, den 29.09.2006, 19:30 -0400 schrieb Tamas K Papp:
  the smallest positive floating point number x such that 1+x /= x?
 That would be the smallest positive number, woudn't it?
 
 Do you mean the smalles postive number x with 1+x /= 1?

Hi Joachim,

Specifically, I would be happy with the smallest Double that makes the
statement true.  I need it as a convergence bound for an iterative
algorithm.  Anyhow, thanks for the clarification, but I would be
happier with an answer.

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] smallest double eps

2006-09-29 Thread Tamas K Papp
On Fri, Sep 29, 2006 at 09:26:27PM -0400, Lennart Augustsson wrote:

 As for using such an eps in a convergence test I'd be very careful.   
 How do you know that your iteration doesn't make the value bounce  
 back and forth with more than eps?

Hi Lennart,

Thanks for the answer, I will try it.

I am not using eps, but rather a value derived from eps by analyzing
the algorithm (eg eps^0.25).  Your answer will help me calculate that
directly.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is Haskell a 5GL?

2006-09-29 Thread Tamas K Papp
On Mon, Sep 25, 2006 at 03:27:32PM +0200, Henning Thielemann wrote:

Hi Henning,

 Actually, laziness allows me to formulate algorithms that look more like
 the specification of the problem than the solution. E.g., I can formulate
 the solution of a differential equation in terms of a power series or time
 series in that way. However I have to put some effort into formulating it
 in a way that works. E.g. I'm only able to solve such equations if it is
 possible to express the second derivative in terms of the first and the
 zeroth one. Computer algebra systems are essentially better here.

In my experience, most people use CAS interactively: they encounter an
integral or a PDE that's difficult to solve, so they type it into
Mathematica (which frequently cannot solve it either, then you go
crazy, numerical, or both ;-).  It is more like a sophisticated
symbolic calculator with a lot of patterns built in for manipulating
expressions.

Mathematica has features of a programming language, but most people I
know are not using those when manipulating formulas, and conversely,
when _programming_ in Mathematica (ie writing code and then executing
it do so something repetitive) they rarely do anything symbolic.

CAS are great for specific purposes, especially for replacing those
tomes which have solutions of equations/ODEs/PDEs/integrals etc in
them, and some CAS have Algol-style flow control and numerical methods
which you can use for solving numerical problems, but the two are
almost never mixed.

Best,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] smallest double eps

2006-09-29 Thread Tamas K Papp
On Fri, Sep 29, 2006 at 06:53:35PM -0700, Chad Scherrer wrote:

 Tamas,
 
 You might want to read Joachim's post more carefully - he's trying to
 help you, and I think he makes a good point.

Chad,

If his point is that there is no smallest positive number, then I
think I understand it, thanks.  I should have said that I was looking
for the smallest positive number Double can represent, but thought
that was clear from the context.

If this is not his point, I'd really appreciate an explanation.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] map (-2) [1..5]

2006-09-08 Thread Tamas K Papp
Hi Brian and others,

I posted the original question because I didn't know how to get map
(-2) working.

Since the original posting, many people have presented _a priori_
arguments about the merits of different approaches, most importantly
whether or not to abandon the unary - operator.  As a Haskell newbie,
I find the special treatment of - ugly, but as it is generally
difficult to convince others about one's aesthetic judgements, I would
like to suggest an approach that might add additional reasons in favor
of and against the unary -.

Even though I am fond of a priori arguments, I think that questions of
syntax should be handled as practical ones.  In most languages,
choosing infix and unary operators is guided by practical
considerations: for example, the infix + exists because typing

plus 1 2

would take longer, make the code unreadable.  In this case, the
decision to make + (nothing but) an infix operator is easy, because
there is no trade-off involved.  But in the case of -, there is a
clear tradeoff (at least in Haskell): if we allow unary -, sections
like (-2) won't work.

I wonder if it would be possible to take a large sample of Haskell
code that people generally consider good (ie written by experienced
programmers) and count (with a script, of course) the occurrences of

(A) unary - 

and 

(B) - used as a binary operator where the programmer had to circumvent
unary -, especially in sections, and including things like

(flip (-) x)
(+ (- x))

and other approaches people use to circumvent the problem.

If A is significantly larger than B, people who wish to retain unary -
would have a good case.  On the other hand, if B  A, then the
removal of unary - should be at least considered.  This would allow us
to compare the amount of inconvenience caused by either approach in
practice.

Best,

Tamas



On Fri, Sep 08, 2006 at 03:30:33PM +0100, Brian Hulley wrote:

 Leaving aside the question of negative literals for the moment, what's so 
 special about unary minus that it warrants a special syntax? For example in 
 mathematics we have x! to represent (factorial x), which is also an 
 important function, yet no-one is arguing that we should introduce a unary 
 postfix operator to Haskell just to support it.
 
 In maths we also have |x| to denote another common function, (abs x), yet 
 afaia everyone is happy to just write (abs x).
 
 Would the elimination of the special case rule for unary minus not make the 
 language easier to understand? What's wrong with typing (negate x) in the 
 rare cases where you can't just re-write the expression to use infix minus 
 instead (ie x + -y === x - y)? Surely most programs in Haskell are not 
 just arithmetic expressions, and while it is convenient to have infix +, -, 
 *, `div`, `mod` for the integers, so you can do indexing over data types 
 and other counting operations, I'd argue that the usual functional 
 notation (eg (exp x) (factorial x) (negate x)) should be sufficient for the 
 other arithmetic operations just as it's deemed sufficient for nearly 
 everything else in Haskell! ;-)
 
 In mathematics, we don't use separate symbols for negative integers,
 and negated positive integers, even though in the underlying
 representation of the integers as equivalence classes of pairs of
 naturals, we can write things like -[(1,0)] = [(0,1)], which expressed
 in ordinary notation just says that -1 = -1. This doesn't bother us,
 because the two things are always equal.
 
 Another thing to note is that all the natural literals are not, as one
 might initially think, plain values, but actually represent the
 embedding of that natural number into the ring (instance of Num), by
 way of 0 and 1. They simply provide a convenient notation for getting
 particular values of many rings, but in many cases, don't get one very
 far at all before other functions must be introduced to construct the
 constant values one wants. While there always is a homomorphism from Z
 to a ring (represented in Haskell by fromInteger), one would get
 similar expressiveness by with just the nullary operators 0 and 1, and
 the unary negation as well as addition and multiplication (albeit with
 an often severe performance hit, and some annoyance, I'm not
 recommending we really do this, simply characterising the purpose of
 numeric literals).
 
 If the performance issue regarding the polymorphic literal -5 meaning
 negate (fromInteger 5) is a problem, it would be easy enough to agree
 for the compiler to find and rewrite literals like that as fromInteger
 (-5) instead, where -5 is the precomputed integer -5. Assuming that
 fromInteger is not broken, that will always mean the same thing
 (because fromInteger is supposed to be a homomorphism). Similarly,
 when the type of (fromInteger x) is known statically to be Integer,
 the compiler can rewrite it as x. In any event, this is a tiny
 constant factor performance hit.
 
 Anyway, the point of all this is that 0,1,2... are not really literals
 at all. 

Re: [Haskell-cafe] how do you debug programs?

2006-09-07 Thread Tamas K Papp
On Wed, Sep 06, 2006 at 11:34:05AM +0200, Pepe Iborra wrote:
 Hi Tamas
 
 There are several ways to debug a Haskell program.
 
 The most advanced ones are based in offline analysis of traces, I
 think Hat [1] is the most up-to-date tool for this. There is a Windows
 port of Hat at [5].
 
 Another approach is to simply use Debug.Trace. A more powerful
 alternative for this approach is Hood [2]. Even if it hasn't been
 updated in some time, Hood works perfectly with the current ghc
 distribution. Even more, Hugs has it already integrated [3]. You can
 simply import Observe and use observations directly in your program.

Dear Pepe,

Thank you for the information.  I finally ended up working with
Debug.Trace, and found the bug very quickly.  I also tried Hood, but
couldn't load it in ghci: import Observe can't find the library, but

% locate Observe
/usr/lib/ghc-6.4.2/hslibs-imports/util/Observe.hi
/usr/lib/ghc-6.4.2/hslibs-imports/util/Observe.p_hi
/usr/lib/hugs/libraries/Hugs/Observe.hs
/usr/lib/hugs/oldlib/Observe.hs

Does importing from hslibs-imports require something special?

Quite a bit of philosophical discussion erupted as a result of my
original question.  I understand the arguments of those who dislike
debuggers, but I don't think I could have done without some debugging.

It turns out that the problem was not in the algorithm, but in the
specification of the equation itself.  I was solving a continuous time
Hamilton-Jacobi-Bellman equation, and there was a point in the state
space where the supremum was infinity, giving stuff like 0/Inf and
their ilk.  I respecified the problem and now it works.

For those who think that it is always possible to break the problem up
into small pieces you can test individually (without a debugger): you
can't, at least not when solving functional equations.  The corner
cases (and nonconvergence, when using non-contraction methods, such as
PEA) are difficult to catch and reproduce.

Thanks for all the suggestions,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] how do you debug programs?

2006-09-06 Thread Tamas K Papp
Hi,

I would like to learn a reasonable way (ie how others do it) to debug
programs in Haskell.  Is it possible to see what's going on when a
function is evaluated?  Eg in

f a b = let c = a+b
d = a*b
in c+d

evaluating 

f 1 2

would output something like

f called with values 1 2
c is now (+) a b = (+) 1 2 = 3
d is now (*) a b = (*) 1 2 = 2
...

Or maybe I am thinking the wrong way, and functional programming has
its own debugging style...

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: how do you debug programs?

2006-09-06 Thread Tamas K Papp
On Wed, Sep 06, 2006 at 06:33:32AM -0400, Lennart Augustsson wrote:
 I've also used Visual Studio, and I wouldn't mind having something  
 like that for Haskell.  But I have to agree with Jon, I think the  
 best way of debugging is to understand your code.  I think people who  
 come from imperative programming come with a mind set that you  
 understand your code by stepping through it in the debugger.  But I  
 find this paradigm much less useful for functional code.

At this point, I need debugging not because I don't understand my
code, but because I don't understand Haskell ;-) Most of the mistakes
I make are related to indentation, precedence (need to remember that
function application binds tightly).  The compiler and the type system
catches some mistakes, but a few remain.

Thanks for the suggestions.

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] does the compiler optimize repeated calls?

2006-09-06 Thread Tamas K Papp
Hi,

I have a question about coding and compilers.  Suppose that a function
is invoked with the same parameters inside another function declaration, eg

-- this example does nothing particularly meaningless
g a b c = let something1 = f a b
  something2 = externalsomething (f a b) 42
  something3 = externalsomething2 (137 * (f a b)) in
  ...

Does it help (performancewise) to have

g a b c = let resultoff = f a b
  something2 = externalsomething resultoff 42
  something3 = externalsomething2 (137 * resultoff) in
  ...

or does the compiler perform this optimization?  More generally, if a
function is invoked with the same parameters again (and it doesn't
involve anything like monads), does does it makes sense
(performancewise) to store the result somewhere?

Thank you,

Tamas

PS: I realize that I am asking a lot of newbie questions, and I would
like to thank everybody for their patience and elucidating replies.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] practice problems?

2006-09-03 Thread Tamas K Papp
Hi,

I am a Haskell newbie.  Having read some tutorials (Yet Another,
Gentle Introduction) and some papers/tutorials on monads, I would like
to spend some time practicing what I have learned before embarking on
more abstract/obscure things and/or using Haskell for everyday tasks.

I am looking for small to medium sized practice problems, preferably
with solutions.  Hal Daume's tutorial had some good one-liners (eg
rewrite something point-free) but I am looking for something which
would take 1-3 hours for a newbie, and then I could look at a solution
by a Haskell expert and discover what I could have done better/more
elegantly.

Online references (or even book suggestions) would be appreciated.  If
you are teaching Haskell and have problems with confidential solution
sets (some instructors like it that way), I can promise not to
disclose the latter ;-)

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] practice problems?

2006-09-03 Thread Tamas K Papp
On Sun, Sep 03, 2006 at 12:47:45PM +0400, Bulat Ziganshin wrote:
 Hello Tamas,
 
 Sunday, September 3, 2006, 12:15:48 PM, you wrote:
 
  I am looking for small to medium sized practice problems, preferably
  with solutions.  Hal Daume's tutorial had some good one-liners (eg
  rewrite something point-free) but I am looking for something which
  would take 1-3 hours for a newbie, and then I could look at a solution
 
 not exactly that you ask but i recommend you hitchhikers guide and
 implementing Scheme in 48 hours as an interesting mini-books for
 advanced newbie. look at books and tutorials page
 
 i also suggest you to start write some library. there is enough useful
 libs that are still bnot implemented because lack of time (and
 insterest in such simple code) on side of more experienced
 programmers. i once proposed you to write strings library, another
 interesting and useful thing will be gzip/bzip2-lib bindings

Bulat,

I would be happy to write a strings library, I just don't know that it
is supposed to do... (I have no CS education, only math/economics).
If you show me the specifications or documentation in another
language, I would write one as practice.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: data structures question

2006-09-01 Thread Tamas K Papp
On Thu, Aug 31, 2006 at 11:09:07AM +0400, Bulat Ziganshin wrote:
 Hello Benjamin,
 
 Wednesday, August 30, 2006, 11:40:09 PM, you wrote:
 
  Matthias Fischmann wrote:
  The trick is that Int is not the only index data type, but tuples of
  index data types are, too.  Do this:
  
  | type Point = (State, State, Int)
  | type TypeV = Array State Double
  | 
  | matrix :: TypeV
  | matrix = array bounds values
  |where
  |...
 
  Surely you meant to say
 
  | type TypeV = Array Point Double
 
 which will require 128 gigs of memory for 32-bit cpus and even
 slightly more for 64-bit ones :)

Bulat,

Can you please explain this?  The following code works fine for me,
and I don't have that much RAM ;-) It seems I am not getting
something.


import Data.Array

data State = Low | High deriving (Eq,Ord,Ix,Show)

other :: State - State
other High = Low
other Low = High

type Point = (State,State,Int)
type TypeV = Array Point Double

f (Low,Low,a) = a

matrix = array ((Low,Low,0),(Low,Low,4)) [(i,f(i)) | i - range 
((Low,Low,0),(Low,Low,4))]


Thank you,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] getContents and lazy evaluation

2006-09-01 Thread Tamas K Papp
Hi,

I am newbie, reading the Gentle Introduction.  Chapter 7
(Input/Output) says

  Pragmatically, it may seem that getContents must immediately read an
  entire file or channel, resulting in poor space and time performance
  under certain conditions. However, this is not the case. The key
  point is that getContents returns a lazy (i.e. non-strict) list of
  characters (recall that strings are just lists of characters in
  Haskell), whose elements are read by demand just like any other
  list. An implementation can be expected to implement this
  demand-driven behavior by reading one character at a time from the
  file as they are required by the computation.

So what happens if I do

contents - getContents handle
putStr (take 5 contents) -- assume that the implementation 
 -- only reads a few chars
-- delete the file in some way
putStr (take 500 contents) -- but the file is not there now

If an IO function is lazy, doesn't that break sequentiality?  Sorry if
the question is stupid.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] data structures question

2006-08-30 Thread Tamas K Papp
Hi,

Having read some tutorials, I would like to start using Haskell for
real, but I have some questions about data structures.

The mathematical description of the problem is the following: assume
there is a function V(a,b,theta), where a and b can have two values,
High or Low, and theta is a number between zero and n (n is given).
The range of V is the real numbers.

Then there is an algorithm (called value iteration, but that's not
important) that takes V and produces a function of the same type,
called V'.  The algorithm uses a mapping that is not elementwise, ie
more than the corresponding values of V are needed to compute a
particular V'(a,b,theta) -- things like V(other a,b,theta) and
V(a,b,theta+1), where

data State = Low | High
other :: State - State
other High = Low
other Low = High

Question 1: V can be represented as a 3-dimensional array, where the
first two indices are of type State, the third is Int (= n).  What
data structure do you suggest in Haskell to store V?  Is there a
multidimensional array or something like this?

Let's call this structure TypeV.

Question 2: I would like to write

valueit :: TypeV - TypeV
valueit V = mapondescartesproduct [Low,High] [Low,High] [0..n] mapV where
-- mapV would calculate the new V' using V
-- mapV :: State - State - Int - Double

to fill the new data structure.  How to do this sensibly?

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] implementing a csv reader

2006-08-22 Thread Tamas K Papp
Hi,

Now that I have read the tutorials, I think that the best way to learn
Haskell would be to use the language and write something simple yet
useful.  I noticed that Haskell lacks a module for reading/writing csv
(comma separated value) files, so I thought I could implement that.

Questions:

1. Please tell me if you know of a csv module, because then I would do
   something else.

2. I am looking for a parser, but I don't know Haskell parsers.  Is
   Parsec a good choice?

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] implementing a csv reader

2006-08-22 Thread Tamas K Papp
On Tue, Aug 22, 2006 at 11:26:45AM +0200, Henning Thielemann wrote:

   See also
  http://www.xoltar.org/languages/haskell.html
  http://www.xoltar.org/languages/haskell/CSV.hs

Thanks.  Haskell is incredibly neat ;-)

Now I need to find something else for practice.  Is there anything
related to data analysis/statistics that is lacking is Haskell? 

Best,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell wiki: most popular pages

2006-08-19 Thread Tamas K Papp
On Fri, Aug 18, 2006 at 04:46:14PM +0400, Bulat Ziganshin wrote:
 Hello Tim,
 
 Friday, August 18, 2006, 4:26:46 PM, you wrote:
 
  break p = span (not . p)
 
 it's definitely better
 
  and = foldr () True
 
 i think that definitions with omitted arguments can be more hrd to
 understand to newbie haskellers, especiallyones who not yet know the
 language. as Tamas suggests, this page can be used to present to such
 newbies taste of Haskell so listing all the parameters may allow to
 omit unnecessary complications in this first look into language

I think I learned to understand and appreciate omitted arguments from
Hal Daume's Yet Another Haskell Tutorial.  The exercises there are
just great.

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] how to generate XHTML 1.1?

2006-08-18 Thread Tamas K Papp
Hi,

I would like to use Haskell to generate static HTML webpages,
preferably using XHTML 1.1.  How can I do this?  I found libraries to
generate XHTML 1.0 transitional, but not 1.1.  Scheme has LAML [1],
which parses the DTD, uses S-expressions and validates the XHTML, I am
looking for something similarly elegant (even if without the
S-expressions, which I haven't seen in Haskell yet).

Thanks,

Tamas

[1] http://www.cs.aau.dk/~normark/laml/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] iterative algorithms: how to do it in Haskell?

2006-08-17 Thread Tamas K Papp
On Thu, Aug 17, 2006 at 01:23:19AM -0400, [EMAIL PROTECTED] wrote:
 G'day all.
 
 Quoting Chris Kuklewicz [EMAIL PROTECTED]:
 
  The compiler may not deforest that list, so creating the list may be a small
  overhead of this method.
 
 And in return, you get:
 
 - Code that is smaller than the imperative version, AND
 - a reusable function, making the next incarnation of
   an algorithm like this even shorter.
 
 For most interesting cases, the cost of f and goOn will surely dominate
 anyway.
 
  Note that f x should be f a above.
 
 Yes, you're right.  I abstracted out f after testing and before
 posting.

Chris, Christian, Andrew, Antti-Juhani and Ivan,

Thanks for your answers, they were very enlightening (though it will
take some time to understand everything).  Haskell looks even more
elegant than Scheme...

Best,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell wiki: most popular pages

2006-08-17 Thread Tamas K Papp
On Thu, Aug 17, 2006 at 01:55:47AM +0400, Bulat Ziganshin wrote:
 Hello haskell-cafe,
 
 The http://haskell.org/haskellwiki/Special:Popularpages page lists
 most popular pages on haskell wiki. I think this list is very
 useful because it shows us what are the questions about Haskell
 people most interested and gives us hints what should be improved in
 first place. The only catch is that most of popular pages are just
 ones listed on the title page. But nevertheless... On the other side,
 we can simplify title page by omitting links to page that are of
 little interest for casual readers (such as History). We can also add
 here links to most popular pages, such as GHC and Hitchhikers guide.
 Below is beginning of this list:
 
 Haskell (252,505 views)
 Introduction (50,091 views) 
 Libraries and tools (41,864 views) 
 Books and tutorials (40,040 views) 
 Language and library specification (32,773 views) 
 Haskell in practice (31,698 views) 
 Implementations (24,141 views) 
 GHC (20,634 views) 
 Haskell in 5 steps (16,707 views) 
 Learning (14,088 views) 
 Hitchhikers guide to Haskell (13,191 views) 
 Future (12,754 views)

A link to A tour of the Haskell Prelude might be very useful for
newbies (like me ;-)

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] map (-2) [1..5]

2006-08-17 Thread Tamas K Papp
The code in the subject generates an error.  I understand why this is
(- is treated as part of the number), but I don't know how to solve
it, ie how to tell Haskell that - is a function/binary operator?

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] iterative algorithms: how to do it in Haskell?

2006-08-16 Thread Tamas K Papp
Hi,

I am a newbie learning Haskell.  I have used languages with functional
features before (R, Scheme) but not purely functional ones without
side-effects.

Most of the programming I do is numerical (I am an economist).  I
would like to know how to implement the iterative algorithm below in
Haskell.

f is an a-a function, and there is a stopping rule 
goOn(a,anext) :: a a - Bool which determines when to stop.  The
algorithm looks like this (in imperative pseudocode):

a = ainit

while (true) {
  anext - f(a)
  if (goOn(a,anext))
 a - anext
  else
 stop and return anext
}

For example, f can be a contraction mapping and goOn a test based on
the metric.  I don't know how to do this in a purely functional
language, especially if the object a is large and I would like it to
be garbage collected if the iteration goes on.

Thank you,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Int-[Char] conversion

2006-08-16 Thread Tamas K Papp
Hi,

I am working through Hal Daume's tutorial, trying to do the exercises.
I can't figure out how to output an integer with putStrLn (or any
other way), I think I need an Int - [Char] conversion but couldn't
find it.  Specifically, in Exercise 3.10, I have the product of
numbers in pp, and would like to do 

putStrLn (Product:  ++ convertnumbertostring(pp))

but I don't know which function does this...

Sorry for the extremely dumb question.

Thanks,

Tamas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe