
Ordinarily I would not post to a list when I've got a program running. But I've found learning Haskell so tough (this is my 4th try in the last two years) that I feel I have to bore everyone with my first non-trivial program. It is Newton's method adapted to give the square root of a number. And all in just one line which I think is an amazing demonstration of the power of Haskell. And it took no more than a couple of minutes to write (directly to the screen too as opposed to the usual many paper drafts with C). It works too, correct to 13 decimal points on my GHC system It is :- my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t)) It is a bit crude though. 20 iterations is a bit arbitrary. I don't suppose there is a easy way to iterate until the results stop changing.

Hi Longesh
But I've found learning Haskell so tough (this is my 4th try in the last two years) that I feel I have to bore everyone with my first non-trivial program.
Well done on getting something going!
my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))
last (take 20) can be reexpressed as:
my_sqrt t = iterate (\n -> n/2 + t/(2 * n)) t !! 19
For information on what !! does (vs last and take), see Hoogle: http://haskell.org/hoogle
It is a bit crude though. 20 iterations is a bit arbitrary. I don't suppose there is a easy way to iterate until the results stop changing.
close (x:y:xs) | abs (x - y) < 0.1 = y close (x:xs) = close xs I don't know how to add it into the one liner though - although I suspect someone here will :) Thanks Neil

Hi
I don't know how to add it into the one liner though - although I suspect someone here will :)
head . head . filter (\(x:y:_) -> abs (x-y) < 0.1)
Doesn't work, I assume you meant (and typo'd): head . head . filter (\(x:y:_) -> abs (x-y) < 0.1) . tails Which does work fine. Thanks Neil

On Fri, 23 Mar 2007, Neil Mitchell wrote:
Hi Longesh
But I've found learning Haskell so tough (this is my 4th try in the last two years) that I feel I have to bore everyone with my first non-trivial program.
Well done on getting something going!
my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))
http://darcs.haskell.org/htam/src/Numerics/ZeroFinder/Newton.hs with inverse t (\x->(x^2,2*x)) t
It is a bit crude though. 20 iterations is a bit arbitrary. I don't suppose there is a easy way to iterate until the results stop changing.
close (x:y:xs) | abs (x - y) < 0.1 = y close (x:xs) = close xs
I don't know how to add it into the one liner though - although I suspect someone here will :)
http://darcs.haskell.org/htam/src/Numerics/Sequence.hs limitDifference :: (Real a) => a -> [a] -> a limitDifference tol xs = case dropWhile ((> tol) . abs . uncurry (-)) $ zip xs (tail xs) of ((_,x):_) -> x [] -> error "limitDifference: Finite sequence, but no element satisfies abort criterion."

Henning Thielemann wrote:
On Fri, 23 Mar 2007, Neil Mitchell wrote:
my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))
http://darcs.haskell.org/htam/src/Numerics/ZeroFinder/Newton.hs
with inverse t (\x->(x^2,2*x)) t
I don't know how to add it into the one liner though - although I suspect someone here will :)
http://darcs.haskell.org/htam/src/Numerics/Sequence.hs
limitDifference :: (Real a) => a -> [a] -> a limitDifference tol xs = case dropWhile ((> tol) . abs . uncurry (-)) $ zip xs (tail xs) of ((_,x):_) -> x [] -> error "limitDifference: Finite sequence, but no element satisfies abort criterion." _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
as a fellow haskell newbie, i would like to take this opportunity to encourage library authors to submit their material to the haskell hackage. the haskell infrastructure is rather big, complex and "organic". (see e.g. http://darcs.haskell.org/tmp/ for an example) it was only by chance that i ran across the (rather amazing) htam library a week ago or so. i hope you agree that these things should not be left to chance ;-) hoogle, google and hackage tend to be the first places where i look for fresh meat. i suppose this also applies to other haskell freshmen. cheers, v.

On Fri, 23 Mar 2007, Vincent Kraeutler wrote:
as a fellow haskell newbie, i would like to take this opportunity to encourage library authors to submit their material to the haskell hackage.
the haskell infrastructure is rather big, complex and "organic". (see e.g. http://darcs.haskell.org/tmp/ for an example) it was only by chance that i ran across the (rather amazing) htam library a week ago or so. i hope you agree that these things should not be left to chance ;-)
hoogle, google and hackage tend to be the first places where i look for fresh meat. i suppose this also applies to other haskell freshmen.
I suspect, that a Hackage package let you think that my sandbox is something fixed and well-done, which is certainly not true. Why didn't Google lead you to http://www.haskell.org/haskellwiki/Libraries_and_tools/Mathematics#Miscellan... ? Of course, I would like to get indexed in haskell.org/hoogle - but how to do that?

G'day all.
Quoting Henning Thielemann
http://darcs.haskell.org/htam/src/Numerics/ZeroFinder/Newton.hs
with inverse t (\x->(x^2,2*x)) t
The problem with all such solutions was touched on by Longesh:
20 iterations is a bit arbitrary.
This isn't a one-liner, but there's a straightforward answer to the "arbitrary" number of iterations: Assuming that the task is to compute a square root fast, a better solution is to come up with an initial guess that's good enough such that you only need a fixed number of iterations. This one is as accurate as my CPU's built-in sqrt, and unlike every other solution posted here, it uses no loops: http://www.opensubscriber.com/message/haskell-cafe@haskell.org/5135386.html But, of course, we're now way off-topic from the original question! Cheers, Andrew Bromage

It is :-
my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))
It is a bit crude though. 20 iterations is a bit arbitrary. I don't suppose there is a easy way to iterate until the results stop changing.
Here's something for you to play with: my_sqrt t = fix (\n -> n/2 + t/(2 * n)) t fix :: (Double -> Double) -> Double -> Double fix f x | x == fx = x | otherwise = f (fix f fx) where fx = f x My numerical analysis is a bit on the dodgy side, but you probably don't want to use == to test for convergence. Maybe you can find a nice way to write fix using some prelude functions? Cheers, Bernie.

On Fri, Mar 23, 2007 at 01:04:47PM +0200, Logesh Pillay wrote:
my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))
It is a bit crude though. 20 iterations is a bit arbitrary. I don't suppose there is a easy way to iterate until the results stop changing.
perhaps
sqrt t = head [ n | (n,True) <- iterate (\n -> let n' = n/2 + t/(2 * n) in (n',n' == n)) t ]
John -- John Meacham - ⑆repetae.net⑆john⑈
participants (8)
-
ajb@spamcop.net
-
Bernie Pope
-
Henning Thielemann
-
John Meacham
-
Ketil Malde
-
Logesh Pillay
-
Neil Mitchell
-
Vincent Kraeutler