Answer the question
In order to leave comments, you need to log in
How to map values to a list in Scheme?
After a year of independent study of Python as the first and main PL, I came to the university for a master's degree, where it is proposed to write tasks in Scheme (R5RS) as a functional PL.
Having got used to writing according to the imperative paradigm, I began to stumble almost immediately.
For example. It is required to write a program that calculates the day of the week from a date. Let the procedure take a year-month-day and return numbers from 0 (Monday) to 6 (Sunday).
After delving into the implementation of such an algorithm, I came across an interesting solution in Python:
def whatDay(day, month, year):
days = ["пн","вт","ср","чт","пт","сб","вс"]
a = (14 - month) // 12
y = year - a
m = month+12 * a-2
result = ((7000 + (day + y + y//4 - y//100 + y//400 + (31*m) // 12)) % 7) - 1
return days[result]
(define (day-of-week day month year)
(list mon, tue, wed, thu, fri, sat, sun)
(define (calculate-a month)
(quotient (- 14 month) 12))
(define (calculate-y year calculate-a)
(- year calculate-a))
(define (calculate-m month calculate-a)
(- (+ (* 12 calculate-a) month) 2)))
(define (calculate-result calculate-y day calculate-m)
(- (remainder
(+ (-
(+ (quotient (* 31 calculate-m) 12)
(quotient calculate-y 400)
(quotient y 4)
day
calculate-y)
(calculate-y 100))
7000)
7)
1)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question