[Haskell-beginners] Square root algorithm
mike h
mike_k_houghton at yahoo.co.uk
Sat Sep 9 07:44:57 UTC 2017
I’m sort of seeing this as a foldr over a list of digit pairs with the seed value of the fold being the first step of the algorithm (which is different from the other steps).
The list of pairs will be of indeterminate length as ’00’ is used until the required number of digits in the result is achieved.
A minor wrinkle with this is if the input number is a perfect square then a lot of ‘0’s would be in the result.
Then, after the fold, apply some simple function to determine where the decimal point should go.
M
> On 9 Sep 2017, at 05:49, KC <kc1956 at gmail.com> wrote:
>
> One approach
>
> One function to compute the next iterate
>
> Another function to call the computation function until results are within some tolerance
>
> It's usually presented as separation of control and computation 😎
>
> --
> Sent from an expensive device which will be obsolete in a few months
> Casey
>
> On Sep 3, 2017 1:23 AM, "mike h" <mike_k_houghton at yahoo.co.uk <mailto:mike_k_houghton at yahoo.co.uk>> wrote:
> Hi,
>
> To help me in learning Haskell I started blogging about some of the things I’ve looked at.
> One such topic was calculating square roots ‘by hand’ and then deriving a Haskell algorithm.
> I wrote about the well known technique here
> http://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/ <http://gitcommit.co.uk/2017/08/25/the-root-of-the-problem-part-1/>
>
> and it it is really quite a simple method.
>
> The second part of the post will be an implementation in Haskell.
>
> I then tried implementing it and got something that works but really its not very pleasant to look at! And its something I don’t want to post! Some parts are fine but I think I locked myself into the notion that it had to be using State and really the end result is pretty poor.
>
> I know this i perhaps a ‘big ask’ but I’d really appreciate any suggestions, solutions, hints etc. I will of course give full attribution.
>
> I’ve created a gist of the code here
> https://gist.github.com/banditpig <https://gist.github.com/banditpig>
>
> Many Thanks
>
> Mike
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org <mailto:Beginners at haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170909/333f3743/attachment-0001.html>
More information about the Beginners
mailing list