Talk:Egyptian division: Difference between revisions

From Rosetta Code
Content added Content deleted
(Trying to get the wiki link right :-))
Line 8: Line 8:


:: Fair enough – it's essentially a hylomorphism [https://en.wikipedia.org/wiki/Hylomorphism_(computer_science)], and it seems reasonable / sufficient to ask people not to fuse the building up and the stripping down to the extent that the intermediate data structure is concealed. In the Haskell, JS, and AppleScript versions I've pulled that out and named it 'rows' rather than fusing it out of sight. I hope that seems enough to you :-) [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 19:09, 10 August 2017 (UTC)
:: Fair enough – it's essentially a hylomorphism [https://en.wikipedia.org/wiki/Hylomorphism_(computer_science)], and it seems reasonable / sufficient to ask people not to fuse the building up and the stripping down to the extent that the intermediate data structure is concealed. In the Haskell, JS, and AppleScript versions I've pulled that out and named it 'rows' rather than fusing it out of sight. I hope that seems enough to you :-) [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 19:09, 10 August 2017 (UTC)
::: A fused composition of the unfold and the fold, in which the rows were implicit but not directly visible, might look something like:
::::<lang Haskell>import Data.List (unfoldr)

egyptianQuotRem :: Integral a => a -> a -> (a, a)
egyptianQuotRem m n =
foldr
(\(i, x) (q, r) ->
if x < r
then (q + i, r - x)
else (q, r))
(0, m)
$ unfoldr
(\(i, x) ->
if x > m
then Nothing
else Just ((i, x), (i + i, x + x)))
(1, n)

main :: IO ()
main = print $ egyptianQuotRem 580 34</lang> [[User:Hout|Hout]] ([[User talk:Hout|talk]]) 19:35, 10 August 2017 (UTC)

Revision as of 19:35, 10 August 2017

Perhaps no need to ask for more than one array ?

The description seems attached to the use of two distinct arrays – does that seem necessary ? ( One list/array of tuples/pairs/records might seem more natural in some languages) Hout (talk) 14:49, 10 August 2017 (UTC)

Indeed. In the Perl6 example, I use an array of pairs rather than two separate arrays. Seems to me that _how_ the values are stored is an implementation detail that isn't critical to the task. --Thundergnat (talk) 15:18, 10 August 2017 (UTC)
Two arrays, one table of "pairs" would be fine. I would like a such a semblance of the description to be used to aid in example comparison . Thanks. --Paddy3118 (talk) 16:24, 10 August 2017 (UTC)
Fair enough – it's essentially a hylomorphism [1], and it seems reasonable / sufficient to ask people not to fuse the building up and the stripping down to the extent that the intermediate data structure is concealed. In the Haskell, JS, and AppleScript versions I've pulled that out and named it 'rows' rather than fusing it out of sight. I hope that seems enough to you :-) Hout (talk) 19:09, 10 August 2017 (UTC)
A fused composition of the unfold and the fold, in which the rows were implicit but not directly visible, might look something like:
<lang Haskell>import Data.List (unfoldr)

egyptianQuotRem :: Integral a => a -> a -> (a, a) egyptianQuotRem m n =

 foldr
      (\(i, x) (q, r) ->
          if x < r
            then (q + i, r - x)
            else (q, r))
      (0, m)
      $ unfoldr
         (\(i, x) ->
             if x > m
               then Nothing
               else Just ((i, x), (i + i, x + x)))
         (1, n)

main :: IO () main = print $ egyptianQuotRem 580 34</lang> Hout (talk) 19:35, 10 August 2017 (UTC)