Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
Credit Card Validator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tc3g20
Credit Card Validator
Commits
a8216d87
Commit
a8216d87
authored
3 years ago
by
Thomas Cutts
Browse files
Options
Downloads
Patches
Plain Diff
Updated readme and added the haskell source code.
parent
af8b63c9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+6
-1
6 additions, 1 deletion
README.md
homework1.hs
+52
-0
52 additions, 0 deletions
homework1.hs
with
58 additions
and
1 deletion
README.md
+
6
−
1
View file @
a8216d87
# Credit Card Validator
Haskell program to validate credit card numbers.
\ No newline at end of file
This is an exercise from https://www.cis.upenn.edu/~cis194/spring13/hw/01-intro.pdf.
I have written a rudimentary checksum formula which could be used to validate credit card numbers.
To compile: ghc -o egOutputFile homework1.hs
To run: ./egOutputFile
This diff is collapsed.
Click to expand it.
homework1.hs
0 → 100644
+
52
−
0
View file @
a8216d87
-- ****EX1****
--toDigits 1234 returns [1,2,3,4]
toDigits
::
Int
->
[
Int
]
toDigits
0
=
[]
toDigits
n
|
n
>=
0
=
toDigits
(
n
`
div
`
10
)
++
[
n
`
mod
`
10
]
|
otherwise
=
[]
--toDigitsRev 1234 returns [4,3,2,1]
toDigitsRev
::
Int
->
[
Int
]
toDigitsRev
0
=
[]
toDigitsRev
n
|
n
>=
0
=
n
`
mod
`
10
:
toDigitsRev
(
n
`
div
`
10
)
|
otherwise
=
[]
-- ****EX2****
--doubles every other no. beginning from the rhs of list
doubleEveryOther
::
[
Int
]
->
[
Int
]
doubleEveryOther
[]
=
[]
doubleEveryOther
(
x
:
y
:
z
)
|
(
length
(
x
:
y
:
z
))
`
mod
`
2
/=
0
=
x
:
y
*
2
:
doubleEveryOther
z
|
otherwise
=
x
*
2
:
y
:
doubleEveryOther
z
-- ****EX3****
-- calculates the sum of all digits
-- e.g [16,7,12,5] = 1+6+7+1+2+5==22
sumDigits
::
[
Int
]
->
Int
sumDigits
[]
=
0
sumDigits
(
x
:
xs
)
|
x
<
10
=
x
+
sumDigits
xs
|
otherwise
=
(
x
`
mod
`
10
)
+
(
x
`
div
`
10
)
+
sumDigits
xs
-- ****EX4****
--function confirms if the number is a valid credit card number
--number is valid if remainder when div by 10 is 0
validate
::
Int
->
Bool
validate
n
|
(
sumDigits
(
doubleEveryOther
(
toDigits
n
)))
`
mod
`
10
==
0
=
True
|
otherwise
=
False
-- main function
main
=
do
putStrLn
(
"Enter your credit card number"
)
a
<-
(
readLn
)
let
b
=
validate
a
if
(
b
==
True
)
then
putStrLn
(
"Valid"
)
else
putStrLn
(
"Invalid"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment