<div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Olivier,<div><br></div><div>I had a quick look, I think it's a great first try. Here are my thoughts!</div><div><br></div><div><br></div><div><br></div><div><div>module DivRusse where</div><div><br></div><div>{-</div><div> Comments:</div><div><br></div><div> * This seems unsafe in that it doesn't handle negative numbers well.</div><div> * This can be evidenced by needing a guard on our property.</div><div> * Could be addressed by using a safe newtype.</div><div> * Define properties and use quickcheck to test them.</div><div> * Favor pattern-matching over use of `fst`, `snd`.</div><div> * Use `where` over `let` to highlight what the final result is.</div><div> * Rewrite folds to more wholemeal approach. e.g. `sum $ map snd filteredPair`</div><div> * Use standard functions and composition to eliminate lambdas like this: `(\(x, _) -> x `mod` 2 /= 0 )` = `(odd . fst)`.</div><div> * `russmulList` could go into an infinite loop for negative numbers. Either prevent this with types (preferred), or return an error somehow.</div><div><br></div><div> -}</div><div><br></div><div>main :: IO ()</div><div>main = do</div><div> putStrLn "13 x 12 is"</div><div> print $ russmul 13 12</div><div><br></div><div>-- Property: Does russmul = *?</div><div>prop_russmul :: Int -> Int -> Bool</div><div>prop_russmul a b</div><div> | a > 0 && b > 0 = russmul a b == a * b</div><div> | otherwise = True</div><div><br></div><div>russmul :: Int -> Int -> Int</div><div>russmul a b = sum $ map snd filteredPair</div><div> where</div><div> filteredPair = filter (odd . fst) $ (a,b) : russmulList a b</div><div><br></div><div>russmulList :: Int -> Int -> [(Int, Int)]</div><div>russmulList 1 _ = []</div><div>russmulList a b = (a', b') : russmulList a' b'</div><div> where</div><div> a' = a `div` 2</div><div> b' = b * 2</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div>Warm Regards,</div><div><br></div><div> - Lyndon</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 10, 2020 at 8:55 PM Olivier Revollat <<a href="mailto:revollat@gmail.com">revollat@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi everybody,</div><div>it's my first message on this ML :)</div><div><br></div><div>I don't know if it's appropriate to post this here but I would like to have some feedback with one of my first Haskell code.</div><div>I've been inspired by a recent Numberphile video (<a href="https://www.youtube.com/watch?v=HJ_PP5rqLg0" target="_blank">https://www.youtube.com/watch?v=HJ_PP5rqLg0</a>) how explain the "Russian Peasant" algorithm to do multiplication (here in a nutshell : <a href="https://www.wikihow.com/Multiply-Using-the-Russian-Peasant-Method" target="_blank">https://www.wikihow.com/Multiply-Using-the-Russian-Peasant-Method</a>)</div><div><br></div><div>So I decided I give it a go in Haskell, here is my solution, I appreciate if you give me some feedback on how to improve this code (make it more "idiomatic Haskell")</div><div><br></div><div>NB : I apologize if it's not the right place to ask this kind of review ... in that case, where can I post this ?</div><div><br></div><div>Thanks !<br></div><div><br></div><div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(86,156,214)">module</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(78,201,176)">DivRusse</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">where</span></div><br><div><span style="color:rgb(220,220,170)">main</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">::</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">IO</span><span style="color:rgb(212,212,212)"> ()</span></div><div><span style="color:rgb(212,212,212)">main = </span><span style="color:rgb(197,134,192)">do</span></div><div><span style="color:rgb(212,212,212)"> putStrLn </span><span style="color:rgb(206,145,120)">"13 x 12 is"</span></div><div><span style="color:rgb(212,212,212)"> print $ russmul </span><span style="color:rgb(181,206,168)">13</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(181,206,168)">12</span></div><br><div><span style="color:rgb(220,220,170)">russmul</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">::</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">-></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">-></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Int</span></div><div><span style="color:rgb(212,212,212)">russmul a b =</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">let</span><span style="color:rgb(212,212,212)"> filteredPair = filter (\pair -> (fst pair) `mod` </span><span style="color:rgb(181,206,168)">2</span><span style="color:rgb(212,212,212)"> /= </span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)"> ) $ (a,b) : russmulList a b</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">in</span><span style="color:rgb(212,212,212)"> foldr (\pair acc -> snd pair + acc) </span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)"> filteredPair</span></div><br><br><div><span style="color:rgb(220,220,170)">russmulList</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">::</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">-></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">-></span><span style="color:rgb(212,212,212)"> [(</span><span style="color:rgb(86,156,214)">Int</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(86,156,214)">Int</span><span style="color:rgb(212,212,212)">)]</span></div><div><span style="color:rgb(212,212,212)">russmulList </span><span style="color:rgb(181,206,168)">1</span><span style="color:rgb(212,212,212)"> _ = </span><span style="color:rgb(86,156,214)">[]</span></div><div><span style="color:rgb(212,212,212)">russmulList a b =</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">let</span><span style="color:rgb(212,212,212)"> a' = a `div` </span><span style="color:rgb(181,206,168)">2</span></div><div><span style="color:rgb(212,212,212)"> b' = b * </span><span style="color:rgb(181,206,168)">2</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">in</span><span style="color:rgb(212,212,212)"> (a', b') : russmulList a' b'</span></div></div></div><div><br></div></div>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>