Discussion:
[Haskell-beginners] Simplified Luhn Algorithm
trent shipley
2017-12-31 04:01:51 UTC
Permalink
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.
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.
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
mrx
2017-12-31 08:49:12 UTC
Permalink
I guess your issue is how to represent the card numbers of arbitrary
length, or? Wouldn't a list work?

Patrik

Den 31 dec 2017 05:03 skrev "trent shipley" <***@gmail.com>:

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.
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.
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
John Lusk
2017-12-31 14:10:28 UTC
Permalink
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.
Post by trent shipley
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
* 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.
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.
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Alex Rozenshteyn
2017-12-31 15:06:36 UTC
Permalink
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.
Post by John Lusk
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.
Post by trent shipley
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
* 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.
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.
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
trent shipley
2018-01-02 08:48:34 UTC
Permalink
luhnDouble = (2*x) `mod` 9 almost works, but produces 0 for 2*9 when the
answer should be 9.

So.

luhnDouble :: Int -> Int
luhnDouble x = let y = (2 * x) in if y > 9
then y - 9
else y

(Note, at this point in the book, the "let" trick has not been introduced.)

luhn :: Int -> Int -> Int -> Int -> Bool
luhn x1 x2 x3 x4 = 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] `mod` 10

(This suggested modification works wonderfully.)

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.
Post by Alex Rozenshteyn
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.
Post by John Lusk
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.
Post by trent shipley
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
* 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.
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.
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
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...