T
T
Timebird2017-09-18 05:39:03
Python
Timebird, 2017-09-18 05:39:03

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]

It is not possible to rebuild this solution under, in fact, Scheme. Tried like this:
(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)

Can you please suggest the following:
- How to match each element in the list with the result of the program execution (that is, to make an analogue of Python's return days[result] )?
- Why even at this stage does the compiler throw an error r5rs:body: no expression in body?
Thanks in advance!
PS: I would like to take this opportunity to ask: why in the modern world use lisp and functional programming in general (except for computer algebra)? It is clear that programs are more portable and fast, but FP is still not popular, it's strange - why?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question