Power Series in Haskell

Power Series in Haskell

In list comprehension syntax you have three main parts. Taking your code as example

[y | y <-xs, ( (x^y) / (factorial y) )]

Beginning from the left, you have what each element in the resulting list should be. In your case, simply y. After the pipe character (|) you continue by specifying how to iterate over the input list. In English for each y in xs.

The last part, and your problem, is the filters. You can put a comma separated list of conditions that all need to be true for the list comprehension not to filter out the current y. Instead of putting a condition there (something that is true or false) you put an expression there that results in a number. However, I assume you dont actually want to filter on anything. Rather, you want to output the result of that expression. So it needs to be to the left of the pipe character.

[(x^y) / (factorial y) | y <-xs]

As far as displaying rational numbers, take a look at the Data.Ratio package http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Ratio.html

If you are interested in doing more with power series in Haskell you should check out a great paper by Douglas McIlroy (of UNIX fame): www.cs.dartmouth.edu/~doug/pearl.ps.gz

There he defines an algebra over power series which allows you to define exponentiation by typing:

expx = 1 + (integral expx)

and also goes into other cool stuff like generating functions.

Power Series in Haskell

Leave a Reply

Your email address will not be published.