
Hello, I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid. I solved it this way : toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10] -- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10) -- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs -- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs -- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0 -- | The main entry point. main :: IO () main = do print $ validate 4012888888881881 Any remarks about this solution. I know there are higher function solutions but that part is not descrived in chapter 1 . Roelof

Roelof, Maybe it's better to reuse toDigits in sumDigits. Kees -----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 10:37 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: [Haskell-beginners] any feedback on this solution Hello, I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid. I solved it this way : toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10] -- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10) -- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs -- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs -- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0 -- | The main entry point. main :: IO () main = do print $ validate 4012888888881881 Any remarks about this solution. I know there are higher function solutions but that part is not descrived in chapter 1 . Roelof _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners ----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15

Why, They have opposite terms. SumDigits takes a array and has a integer as output. ToDigits takes a integer and puts out a array. Roelof Kees Bleijenberg schreef op 7-2-2015 om 10:56:
Roelof,
Maybe it's better to reuse toDigits in sumDigits.
Kees
-----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 10:37 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: [Haskell-beginners] any feedback on this solution
Hello,
I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid.
I solved it this way :
toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
-- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
-- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs
-- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs
-- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0
-- | The main entry point. main :: IO () main = do print $ validate 4012888888881881
Any remarks about this solution.
I know there are higher function solutions but that part is not descrived in chapter 1 .
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I'm sure you don't mean array here, but a list! The difference is crucial!
Am 07.02.2015 11:01 schrieb "Roelof Wobben"
Why,
They have opposite terms.
SumDigits takes a array and has a integer as output. ToDigits takes a integer and puts out a array.
Roelof
Kees Bleijenberg schreef op 7-2-2015 om 10:56:
Roelof,
Maybe it's better to reuse toDigits in sumDigits.
Kees
-----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 10:37 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: [Haskell-beginners] any feedback on this solution
Hello,
I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid.
I solved it this way :
toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
-- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
-- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs
-- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs
-- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0
-- | The main entry point. main :: IO () main = do print $ validate 4012888888881881
Any remarks about this solution.
I know there are higher function solutions but that part is not descrived in chapter 1 .
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Nope they don't! Lists and arrays are conceptionally very different!
An array is a fixed block of memory where the single elements are lined up
in order, you have constant time access here, but changing the size
requires you to completely reallocate the memory and copy old content over.
A list on the other hand side is dynamic sized, this is bought by a larger
memory footprint per element (pointer to the next one is added) and higher
time for random access.
Am 07.02.2015 11:59 schrieb "Roelof Wobben"
You are right. Im was confusing it because several languages uses these words differencely.
Roelof
Norbert Melzer schreef op 7-2-2015 om 11:41:
I'm sure you don't mean array here, but a list! The difference is crucial! Am 07.02.2015 11:01 schrieb "Roelof Wobben"
: Why,
They have opposite terms.
SumDigits takes a array and has a integer as output. ToDigits takes a integer and puts out a array.
Roelof
Kees Bleijenberg schreef op 7-2-2015 om 10:56:
Roelof,
Maybe it's better to reuse toDigits in sumDigits.
Kees
-----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 10:37 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: [Haskell-beginners] any feedback on this solution
Hello,
I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid.
I solved it this way :
toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
-- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
-- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs
-- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs
-- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0
-- | The main entry point. main :: IO () main = do print $ validate 4012888888881881
Any remarks about this solution.
I know there are higher function solutions but that part is not descrived in chapter 1 .
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sat, Feb 7, 2015 at 6:09 PM, Norbert Melzer
Lists and arrays are conceptionally very different!
It's fair to point out that e.g. javascript arrays are list-like, as I understand them. They afford dynamic resizing that's seamlessly part of the traditional array API. Other languages may conflate them that way too, I don't know. -- Kim-Ee

Roelof, Say, you find after a few months a bug or a better algorithm for toDigits, then you have to remember that sumDigits uses this algorithm (in a bit different form) too. In this case the functions are so small that it is not a problem. If you apply toDigits to all elements of a list of int's and you get almost the solution apply toDigits to alle elements of [4,12,3] and you get [[4],[1,2],[3]], which is a small step to [4,1,2,3] Kees -----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 11:02 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: Re: [Haskell-beginners] any feedback on this solution Why, They have opposite terms. SumDigits takes a array and has a integer as output. ToDigits takes a integer and puts out a array. Roelof Kees Bleijenberg schreef op 7-2-2015 om 10:56:
Roelof,
Maybe it's better to reuse toDigits in sumDigits.
Kees
-----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 10:37 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: [Haskell-beginners] any feedback on this solution
Hello,
I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid.
I solved it this way :
toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
-- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
-- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs
-- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs
-- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0
-- | The main entry point. main :: IO () main = do print $ validate 4012888888881881
Any remarks about this solution.
I know there are higher function solutions but that part is not descrived in chapter 1 .
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners ----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15

Roelof,
It doesn't look like you ever call toDigitsRev. To keep the solution
clearer, you should remove any unneeded definitions.
Joel
On Sat, 7 Feb 2015 07:50 Kees Bleijenberg
Roelof,
Say, you find after a few months a bug or a better algorithm for toDigits, then you have to remember that sumDigits uses this algorithm (in a bit different form) too. In this case the functions are so small that it is not a problem.
If you apply toDigits to all elements of a list of int's and you get almost the solution apply toDigits to alle elements of [4,12,3] and you get [[4],[1,2],[3]], which is a small step to [4,1,2,3]
Kees
-----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 11:02 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: Re: [Haskell-beginners] any feedback on this solution
Why,
They have opposite terms.
SumDigits takes a array and has a integer as output. ToDigits takes a integer and puts out a array.
Roelof
Kees Bleijenberg schreef op 7-2-2015 om 10:56:
Roelof,
Maybe it's better to reuse toDigits in sumDigits.
Kees
-----Oorspronkelijk bericht----- Van: Beginners [mailto:beginners-bounces@haskell.org] Namens Roelof Wobben Verzonden: zaterdag 7 februari 2015 10:37 Aan: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Onderwerp: [Haskell-beginners] any feedback on this solution
Hello,
I finally solved exercise 1 where I had to write a programm which checks if a creditcard number is valid.
I solved it this way :
toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | n < 10 = [n] | otherwise = toDigits (n `div` 10) ++ [n `mod` 10]
-- | convert a number to a reversed array where a negative number will be a empty array toDigitsRev :: Integer -> [Integer] toDigitsRev 0 = [0] toDigitsRev n | n < 0 = [] | n < 10 = [n] | otherwise = n `mod` 10 : toDigitsRev (n `div` 10)
-- | Doubles every second number from the right. doubleEveryOther :: [Integer] -> [Integer] doubleEveryOther [] = [] doubleEveryOther (x:[]) = [x] doubleEveryOther (x:(y:zs)) | length (x:(y:zs)) `mod` 2 /= 0 = [x] ++ (y * 2) : doubleEveryOther zs | otherwise = [x *2] ++ y : doubleEveryOther zs
-- | sum all the digits of a array sumDigits :: [Integer] -> Integer sumDigits [] = 0 sumDigits (x:zs) | x < 10 = x + sumDigits zs | otherwise = x `mod` 10 + x `div` 10 + sumDigits zs
-- | validate a number by looking if a number can be divided by 10 validate :: Integer -> Bool validate n = sumDigits(doubleEveryOther(toDigits(n))) `mod` 10 == 0
-- | The main entry point. main :: IO () main = do print $ validate 4012888888881881
Any remarks about this solution.
I know there are higher function solutions but that part is not descrived in chapter 1 .
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
----- No virus found in this message. Checked by AVG - www.avg.com Version: 2015.0.5646 / Virus Database: 4281/9071 - Release Date: 02/07/15
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

You should probably not remove toDigitsRev, especially since it's normally
a piece of the correct solution for sumDigits... Your current sumDigits is
incorrect, try it on [100, 100].
On Sat, Feb 7, 2015 at 5:01 PM, Roelof Wobben
Oke,
toDigitsRev was one of the steps of this exercise. but I can remove it.
Roelof
Joel Williamson schreef op 7-2-2015 om 16:56:
Roelof,
It doesn't look like you ever call toDigitsRev. To keep the solution clearer, you should remove any unneeded definitions.
Joel
-- Jedaï

On the other hand it will work here since the source is a list of digits
which have been doubled every two elements...
On Sat, Feb 7, 2015 at 5:06 PM, Chaddaï Fouché
You should probably not remove toDigitsRev, especially since it's normally a piece of the correct solution for sumDigits... Your current sumDigits is incorrect, try it on [100, 100].
On Sat, Feb 7, 2015 at 5:01 PM, Roelof Wobben
wrote: Oke,
toDigitsRev was one of the steps of this exercise. but I can remove it.
Roelof
Joel Williamson schreef op 7-2-2015 om 16:56:
Roelof,
It doesn't look like you ever call toDigitsRev. To keep the solution clearer, you should remove any unneeded definitions.
Joel
-- Jedaï
participants (6)
-
Chaddaï Fouché
-
Joel Williamson
-
Kees Bleijenberg
-
Kim-Ee Yeoh
-
Norbert Melzer
-
Roelof Wobben