Haskell How to find next day? - haskell

-- | Days of week.
data Day = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday deriving (Eq, Show)
-- | Returns the next weekday (excluding weekend, namely Saturday and Sunday).
nextWeekday :: Day -> Day
nextWeekday day =
if x == Sunday
then let x = Monday
if x == Monday
then let x = Tuesday
if x == Tuesday
then let x = Wednesday
if x == Wednesday
then let x = Thursday
if x == Thursday
then let x = Friday
if x == Friday
then let x = Saturday
else
then let x = Sunday
This my code right now... What's wrong with this code...? How can I auto-indent the Haskell code?

An if … then … else … in Haskell always has an else clause, since you write an expression and for the two possible outcomes of the condition, it should return a value.
Furthermore you use if x == Monday, but x is not an input parameter. You also use let x = Monday, but then you need to use an in clause. While let x = Monday in x is valid, we can simply use Monday instead of working with a helper variable x.
You thus can implement this as:
nextWeekday :: Day -> Day
nextWeekday day =
if day == Sunday
then Monday
else if day == Monday
then Tuesday
else if day == Tuesday
then Wednesday
else if day == Wednesday
then Thursday
else if day == Thursday
then Friday
else if day == Friday
then Saturday
else Sunday
Often it is however better to work with pattern matching than branching, so we can implement this as:
nextWeekDay :: Day -> Day
nextWeekDay Sunday = Monday
nextWeekDay Monday = Tuesday
nextWeekDay Tuesday = Wednesday
nextWeekDay Wednesday = Thursday
nextWeekDay Thursday = Friday
nextWeekDay Friday = Saturday
nextWeekDay Saturday = Sunday

Related

Cron: every 44th day of the year

How to compose a cron string for each some day number of year, for instance, each 44th day or each 256th day of year?
0 0 44 * * doesn't work

Select Data based on more than one weekday name in Python

I am trying to get data for weekday Sunday and Monday, but it only give me one day's data. I can find answer for one weekday name from a question asked by somebody.
Below are the code:
import pandas as pd
df=pd.DataFrame({'CustomerID':[1,2,3,4,5,6,7,8,9,10],
'PurchaseDate':['2007-5-7','2007-6-7','2007-7-7','2007-8-7','2007-9-9','2007-10-7',
'2007-11-7','2007-12-7','2008-1-7','2008-2-7' ],
'OrderQuantity':[1,1,1,1,1,1,1,1,1,1]})
df['PurchaseDate']=pd.to_datetime(df.PurchaseDate)
df.dtypes
df.PurchaseDate.dt.weekday_name.value_counts()
df1=df[(df.PurchaseDate.dt.weekday_name==('Sunday' and 'Monday'))]
The result I got is as in the picture below:
How would I get data for Sunday and Monday?
Use Series.isin if want weekday_name Sunday OR Monday - each date cannot be Sunday and also Monday:
df1=df[(df.PurchaseDate.dt.weekday_name.isin(['Sunday','Monday']))]
print (df1)
CustomerID PurchaseDate OrderQuantity
0 1 2007-05-07 1
4 5 2007-09-09 1
5 6 2007-10-07 1
8 9 2008-01-07 1
Verify:
print (df.PurchaseDate.dt.weekday_name)
0 Monday
1 Thursday
2 Saturday
3 Tuesday
4 Sunday
5 Sunday
6 Wednesday
7 Friday
8 Monday
9 Thursday
Name: PurchaseDate, dtype: object

How to display all birthday days till 2025 starting from originating year

from datetime import date,time,timedelta
d = int(input("Enter the year:"))
while (d < 2025):
d += 1
dt = date(month = 4, day = 6, year = d)
print(d,":",dt.strftime("%A"))
I have coded to get all my birthday days till 2025.But how to start with originating year
output:
Enter the year: 1997
1998 : Monday
1999 : Tuesday
2000 : Thursday
2001 : Friday
2002 : Saturday
2003 : Sunday
2004 : Tuesday
2005 : Wednesday
2006 : Thursday
2007 : Friday
2008 : Sunday
2009 : Monday
2010 : Tuesday
2011 : Wednesday
2012 : Friday
2013 : Saturday
2014 : Sunday
2015 : Monday
2016 : Wednesday
2017 : Thursday
2018 : Friday
2019 : Saturday
2020 : Monday
2021 : Tuesday
2022 : Wednesday
2023 : Thursday
2024 : Saturday
2025 : Sunday
Can you try by increasing the count after you're printing the date ?
from datetime import date,time,timedelta
d = int(input("Enter the year:"))
while (d < 2025):
dt = date(month = 4, day = 6, year = d)
print(d,":",dt.strftime("%A")) # print current year
d += 1 # then increment to +1

How to convert Date to days in Haskell?

So I have a problem... I have to write a programm for my college course where I count the exact number of days
so for example
the date January 1st, 0001 will be 1
and the July 26nd, 2018 will be 736412
Beneath I tried to solve this problem, but no luck. Can you point me to my mistake?
type Date = (Int, Int, Int)
type Year = Int
type Month = Int
type Day = Int
datetoday :: Date -> Int
datetoday (year, month , day) = day + monthtoday month + yeartoday year
yeartoday :: year -> day
yeartoday year = ((year-1)*365)
monthtoday :: month -> day
monthtoday month
|month 1 = 0
|month 2 = 31
|month 3 = 59
|month 4 = 90
|month 5 = 120
|month 6 = 151
|month 7 = 181
|month 8 = 211
|month 9 = 243
|month 10 = 273
|month 11 = 304
|month 12 = 334
There are two problems with your code:
yeartoday :: year -> day
year and day should be capitalised. If you don't, this is equivalent to a -> b, since uncapitalised identifiers are seen as type variables. This applies to your other signatures. So, this should be:
yeartoday :: Year -> Day
And the same for the other signatures.
Here's the second problem.
monthtoday month
|month 1 = 0
|month 2 = 31
(...)
The part where you write month 1, month 2 etc. expects a Bool, so you need to compare month and each value, so this should be:
monthtoday month
|month == 1 = 0
|month == 2 = 31
(...)
But even better, you should rewrite this as:
monthtoday month = case month of
1 -> 0
2 -> 31
(..)
There are other errors to do with correctness, and there are better ways of doing this, but I'll leave this to you since the issue here is to do with the type system.

Excel Formula to get Accounting Day like Day 0, Day 1, Day 2 till Day 5 Excluding Weekends

Date Status LastWorkingDate
7/3/2017 Day 0 7/3/2017
7/1/2017 Day 1 7/3/2017
7/2/2017 Day 1 7/3/2017
6/30/2017 Day 1 7/3/2017
6/29/2017 Day 2 7/3/2017
6/28/2017 Day 3 7/3/2017
6/27/2017 Day 4 7/3/2017
6/26/2017 Day 5 7/3/2017
6/25/2017 Day 6 7/3/2017
6/24/2017 Day 6 7/3/2017
6/23/2017 Day 6 7/3/2017
6/22/2017 More than Day 6 7/3/2017
7/4/2017 Day 0 7/4/2017
7/3/2017 Day 1 7/4/2017
7/2/2017 Day 2 7/4/2017
7/1/2017 Day 2 7/4/2017
6/30/2017 Day 2 7/4/2017
6/29/2017 Day 3 7/4/2017
6/28/2017 Day 4 7/4/2017
6/27/2017 Day 5 7/4/2017
6/26/2017 Day 6 7/4/2017
6/25/2017 More than Day 6 7/4/2017
i have tried using =
IF(NETWORKDAYS(E21,G21)-1=0,"day 0",IF(NETWORKDAYS(E21,G21)-1=1,"Day 1",IF(NETWORKDAYS(E21,G21)-1=2,"Day 2",IF(NETWORKDAYS(E21,G21)-1=3,"Day 3",IF(NETWORKDAYS(E21,G21)-1=4,"Day 4",IF(NETWORKDAYS(E21,G21)-1=5,"Day 5","Greater than 5 Days"))))))
but not getting desired output.
All i want is Day 0 to Day 5 based on two date columns(Date and LAstWorkingDate).
Day 0 = if today is monday then lastworkingdate will be friday and friday, Sat and Sunday will become Day 0 and previous week's thursday will be Day 1 and so on
Day 1 = if today is Tuesday then Lastworking Date will be Monday and Monday will become Day 0, Friday,Sat and Sunday will be Day 1 and so on
Day 2 = if today is wednesday ten Lastworkind Date will be Tuesday and Tuesday will become Day 0, Monday - Day 1, Friday, Sat and Sunday wull be Day 2 and so on
.
.
.
How about:
="Day "&(NETWORKDAYS(IF(WEEKDAY(A1,2)=7,A1-2,IF(WEEKDAY(A1,2)=6,A1-1,A1)),C1)-1)
Using your current layout for Last Working Day and Date.
The weekday functions are needed because otherwise the Saturday and Sunday would get the same value as Monday instead of Friday.
Of course you can wrap the whole thing in an IF-formula to make sure you display "Greater than 5 days" when the value is bigger than 5.
Output:
Date | Formula column | Last working day
--------------------------------------------
6/17/2017| Day 11 | 7/3/2017 'Weekend
6/18/2017| Day 11 | 7/3/2017 'Weekend
6/19/2017| Day 10 | 7/3/2017
6/20/2017| Day 9 | 7/3/2017
6/21/2017| Day 8 | 7/3/2017
6/22/2017| Day 7 | 7/3/2017
6/23/2017| Day 6 | 7/3/2017
6/24/2017| Day 6 | 7/3/2017 'Weekend
6/25/2017| Day 6 | 7/3/2017 'Weekend
6/26/2017| Day 5 | 7/3/2017
6/27/2017| Day 4 | 7/3/2017
6/28/2017| Day 3 | 7/3/2017
6/29/2017| Day 2 | 7/3/2017
6/30/2017| Day 1 | 7/3/2017
7/1/2017 | Day 1 | 7/3/2017 'Weekend
7/2/2017 | Day 1 | 7/3/2017 'Weekend
7/3/2017 | Day 0 | 7/3/2017

Resources