-
-
Notifications
You must be signed in to change notification settings - Fork 359
[Haskell] Computus #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
berquist
merged 7 commits into
algorithm-archivists:master
from
jiegillet:haskell-computus
Jul 11, 2020
Merged
[Haskell] Computus #679
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bf90ad
Added Haskell
jiegillet 716773b
Fixed f
jiegillet 2aacc44
Different arrangement
jiegillet d75f37f
Adding type annotations and format with ormolu
jiegillet d839109
Merge branch 'master' into haskell-computus
leios b58aebb
Type signature on seperate line
jiegillet ba79eb2
Merge branch 'haskell-computus' of github.com:jiegillet/algorithm-arc…
jiegillet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
data Mode = Servois | Easter | ||
|
||
computus :: Mode -> Int -> String | ||
computus mode year = | ||
case mode of | ||
Servois -> | ||
-- Value for Servois' table | ||
show $ (21 + d) `mod` 31 | ||
Easter -> | ||
-- Determination of the correct month for Easter | ||
if 22 + d + f > 31 | ||
then "April " ++ show (d + f - 9) | ||
else "March " ++ show (22 + d + f) | ||
where | ||
a, b, c, d, e, f, k, m, n, p, q :: Int | ||
-- Year's position on the 19 year metonic cycle | ||
a = year `mod` 19 | ||
-- Century index | ||
k = year `div` 100 | ||
-- Shift of metonic cycle, add a day offset every 300 years | ||
p = (13 + 8 * k) `div` 25 | ||
-- Correction for non-observed leap days | ||
q = k `div` 4 | ||
-- Correction to starting point of calculation each century | ||
m = (15 - p + k - q) `mod` 30 | ||
-- Number of days from March 21st until the full moon | ||
d = (19 * a + m) `mod` 30 | ||
-- Finding the next Sunday | ||
-- Century-based offset in weekly calculation | ||
n = (4 + k - q) `mod` 7 | ||
-- Correction for leap days | ||
b = year `mod` 4 | ||
c = year `mod` 7 | ||
-- Days from d to next Sunday | ||
e = (2 * b + 4 * c + 6 * d + n) `mod` 7 | ||
-- Historical corrections for April 26 and 25 | ||
f = | ||
if (d == 29 && e == 6) || (d == 28 && e == 6 && a > 10) | ||
then -1 | ||
else e | ||
|
||
-- Here, we will output the date of the Paschal full moon | ||
-- (using Servois notation), and Easter for 2020-2030 | ||
main :: IO () | ||
main = do | ||
let years :: [Int] | ||
years = [2020 .. 2030] | ||
servoisNumbers, easterDates :: [String] | ||
servoisNumbers = map (computus Servois) years | ||
easterDates = map (computus Easter) years | ||
putStrLn "The following are the dates of the Paschal full moon (using Servois notation) and the date of Easter for 2020-2030 AD:" | ||
putStrLn "Year\tServois number\tEaster" | ||
let conc :: Int -> String -> String -> String | ||
conc y s e = show y ++ "\t" ++ s ++ "\t\t" ++ e | ||
mapM_ putStrLn $ zipWith3 conc years servoisNumbers easterDates |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type signatures are recomended for local bindings. They help you think and avoid crazy type errors on basic mistakes (as ghc will just try make it work somehow).
So consider adding:
a :: Int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that achieve anything? The signature of
mod
ismod :: Integral a => a -> a -> a
and sinceyear
is anInt
,a
cannot possibly be anything else.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you make a mistake on writing implementations, having your code blotted with typesignatures will avoid crazy ghc deductions. It's more defensive. Typesignatures rarely do anything in haskell (1). It'sHindley–Milner all the way down.
(1): Bar some major exceptions from langauge extensions, such as datakinds, GADTs etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it would hinder readability I think. And since all the local bindings derive from
year
, it would do nothing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind going either way here. It looks like this conversation is the only one holding this PR back.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see the confusion, how about this:
let blocks can have same typesignatures as functions. In fact, you can put functions in let blocks!
Don't worry about being stubborn, this is constructive in my opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this conversation is still going, so let me know when both of you are happy with the code!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this resolved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, I"m not getting a reply so yes?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did reply, on May 24, on another comment. I tried what you suggested (with a twist).
EDIT: nope, that comment was for something else. I pushed a commit but did not comment. Sorry about that.