<div dir="ltr"><div><br></div><div>luhnDouble = (2*x) `mod` 9 almost works, but produces 0 for 2*9 when the answer should be 9.</div><div><br></div><div>So.</div><div><br></div><div>luhnDouble :: Int -> Int</div><div>luhnDouble x = let y = (2 * x) in if y > 9</div><div> then y - 9</div><div> else y</div><div><br></div><div>(Note, at this point in the book, the "let" trick has not been introduced.)</div><div><br></div><div>luhn :: Int -> Int -> Int -> Int -> Bool</div><div>luhn x1 x2 x3 x4 = 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] `mod` 10</div><div><br></div><div>(This suggested modification works wonderfully.)</div><div><br></div><div>A solution for a list of arbitrary length awaits chapter 7, this question was from chapter 4. Note, Wikipedia.en implies that originally the digits of the multiplication result were to be added, as in 8 * 2 = 16 = 1 + 6 = 7 OR 16 - 9 = 7. </div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Dec 31, 2017 at 8:09 AM Alex Rozenshteyn <<a href="mailto:rpglover64@gmail.com">rpglover64@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Haskell can totally return the result of the "==", and that would be one of my suggestions as well. The other suggestion is for "luhnDouble": I would just compute `rem (2 * x) 9`, but if you need to explicitly subtract, you can do `let d = 2 * x in if d > 9 then d - 9 else d`, which does the computation just once.</div><div dir="ltr"><br><div><div><br><div class="gmail_quote"><div dir="ltr">On Sun, Dec 31, 2017 at 9:12 AM John Lusk <<a href="mailto:johnlusk4@gmail.com" target="_blank">johnlusk4@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Looks fine to me. Maybe drop the if-then, and simply return the result of the == ? (Maybe not possible in Haskell (I'm just a duffer myself) but extraneous trues and falses always drive me nuts.)<br><br><div data-smartmail="gmail_signature">--<br>Sent from my tablet, which has a funny keyboard. Makes me sound more curt and muted than normal. </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Dec 30, 2017 11:03 PM, "trent shipley" <<a href="mailto:trent.shipley@gmail.com" target="_blank">trent.shipley@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I have the following, and it works, but I am trying teach myself Haskell, and I have the suspicion that my solutions is both inefficient and graceless. Any feedback would be appreciated.<div><br></div><div>Trent.</div><div><br></div><div>------------------------------------<br><div><br></div><div><div>{-</div><div>8.The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds as follows: </div><div><br></div><div>* consider each digit as a separate number; </div><div>* moving left, double every other number from the second last; </div><div>* subtract 9 from each number that is now greater than 9; </div><div>* add all the resulting numbers together; </div><div>* if the total is divisible by 10, the card number is valid. </div><div><br></div><div>Define a function luhnDouble :: Int -> Int that doubles a digit</div><div>and subtracts 9 if the result is greater than 9. </div><div><br></div><div>For example: </div><div><br></div><div>> luhnDouble 3 </div><div>6</div><div><br></div><div>> luhnDouble 6 </div><div>3 </div><div><br></div><div>Using luhnDouble and the integer remainder function mod, define a function </div><div>luhn :: Int -> Int -> Int -> Int -> Bool </div><div>that decides if a four-digit bank card number is valid. </div><div><br></div><div>For example: </div><div>> luhn 1 7 8 4 </div><div>True</div><div><br></div><div>> luhn 4 7 8 3 </div><div>False </div><div><br></div><div>In the exercises for chapter 7 we will consider a more general version of this function that accepts card numbers of any length.</div><div><br></div><div>Hutton, Graham. Programming in Haskell (pp. 45-46). Cambridge University Press. Kindle Edition. </div><div>-}</div><div><br></div><div>luhnDouble :: Int -> Int</div><div>luhnDouble x = if (2 * x) > 9</div><div> then (2 * x) - 9</div><div> else 2 * x</div><div><br></div><div><br></div><div>luhn :: Int -> Int -> Int -> Int -> Bool</div><div>luhn x1 x2 x3 x4 = if 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] `mod` 10</div><div> then True</div><div> else False</div><div><br></div><div><br></div><div><br></div><div> </div><div><br></div></div></div></div>
<br>_______________________________________________<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>
<br></blockquote></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></div></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>