Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Simplified Luhn Algorithm (John Lusk)
   2. Re:  Simplified Luhn Algorithm (Alex Rozenshteyn)


----------------------------------------------------------------------

Message: 1
Date: Sun, 31 Dec 2017 09:10:28 -0500
From: John Lusk <johnlu...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Simplified Luhn Algorithm
Message-ID:
        <cajqkmbbfkxcrs0f8drzceo0jrvtrcmf9hkdj7yyy7++0kda...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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.)

--
Sent from my tablet,  which has a funny keyboard. Makes me sound more curt
and muted than normal.

On Dec 30, 2017 11:03 PM, "trent shipley" <trent.ship...@gmail.com> wrote:

> 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.
>
> Trent.
>
> ------------------------------------
>
> {-
> 8.The Luhn algorithm is used to check bank card numbers for simple errors
> such as mistyping a digit, and proceeds as follows:
>
> * consider each digit as a separate number;
> * moving left, double every other number from the second last;
> * subtract 9 from each number that is now greater than 9;
> * add all the resulting numbers together;
> * if the total is divisible by 10, the card number is valid.
>
> Define a function luhnDouble :: Int -> Int that doubles a digit
> and subtracts 9 if the result is greater than 9.
>
> For example:
>
> > luhnDouble 3
> 6
>
> > luhnDouble 6
> 3
>
> Using luhnDouble and the integer remainder function mod, define a function
> luhn :: Int -> Int -> Int -> Int -> Bool
> that decides if a four-digit bank card number is valid.
>
> For example:
> > luhn 1 7 8 4
> True
>
> > luhn 4 7 8 3
> False
>
> In the exercises for chapter 7 we will consider a more general version of
> this function that accepts card numbers of any length.
>
> Hutton, Graham. Programming in Haskell (pp. 45-46). Cambridge University
> Press. Kindle Edition.
> -}
>
> luhnDouble :: Int -> Int
> luhnDouble x = if (2 * x) > 9
>     then (2 * x) - 9
>     else 2 * x
>
>
> luhn :: Int -> Int -> Int -> Int -> Bool
> luhn x1 x2 x3 x4 = if 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] `mod`
> 10
>     then True
>     else False
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@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/20171231/224ca06c/attachment-0001.html>

------------------------------

Message: 2
Date: Sun, 31 Dec 2017 15:06:36 +0000
From: Alex Rozenshteyn <rpglove...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Simplified Luhn Algorithm
Message-ID:
        <CALm==bxw5zmaas8ksazla_1oi9t6s2kkqq82zbieb1uzktd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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.

On Sun, Dec 31, 2017 at 9:12 AM John Lusk <johnlu...@gmail.com> wrote:

> 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.)
>
> --
> Sent from my tablet,  which has a funny keyboard. Makes me sound more curt
> and muted than normal.
>
> On Dec 30, 2017 11:03 PM, "trent shipley" <trent.ship...@gmail.com> wrote:
>
>> 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.
>>
>> Trent.
>>
>> ------------------------------------
>>
>> {-
>> 8.The Luhn algorithm is used to check bank card numbers for simple errors
>> such as mistyping a digit, and proceeds as follows:
>>
>> * consider each digit as a separate number;
>> * moving left, double every other number from the second last;
>> * subtract 9 from each number that is now greater than 9;
>> * add all the resulting numbers together;
>> * if the total is divisible by 10, the card number is valid.
>>
>> Define a function luhnDouble :: Int -> Int that doubles a digit
>> and subtracts 9 if the result is greater than 9.
>>
>> For example:
>>
>> > luhnDouble 3
>> 6
>>
>> > luhnDouble 6
>> 3
>>
>> Using luhnDouble and the integer remainder function mod, define a
>> function
>> luhn :: Int -> Int -> Int -> Int -> Bool
>> that decides if a four-digit bank card number is valid.
>>
>> For example:
>> > luhn 1 7 8 4
>> True
>>
>> > luhn 4 7 8 3
>> False
>>
>> In the exercises for chapter 7 we will consider a more general version of
>> this function that accepts card numbers of any length.
>>
>> Hutton, Graham. Programming in Haskell (pp. 45-46). Cambridge University
>> Press. Kindle Edition.
>> -}
>>
>> luhnDouble :: Int -> Int
>> luhnDouble x = if (2 * x) > 9
>>     then (2 * x) - 9
>>     else 2 * x
>>
>>
>> luhn :: Int -> Int -> Int -> Int -> Bool
>> luhn x1 x2 x3 x4 = if 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4]
>> `mod` 10
>>     then True
>>     else False
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners@haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>> _______________________________________________
> Beginners mailing list
> Beginners@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/20171231/8ffcaa5f/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 115, Issue 1
*****************************************

Reply via email to