0

I have a function that tries to extract the year from date strings stored in an alist below:

(setq entry
 '(("date" . "1998/03")
   ("origdate" . "1930")))

(defun extract-year (date)
  (let ((string (cdr (assoc (symbol-name 'date) entry))))
  (progn
    (string-match
     "\\([0-9]\\{4\\}\\)[/-年\\.]?\\([0-9]+\\)?[/-月\\.]?\\([0-9]+\\)?[/-日\\.]?"
     string)
    (match-string 1 string))))

I would like to call, for example, (extract-year date) to get "1998" and (extract-year origdate) to get "1930".

7
  • 1
    You should simply pass the string, e.g., (extract-year "date"). – xuchunyang Nov 8 '20 at 11:53
  • 1
    So (assoc (symbol-name 'date) entry) should simply be (assoc date entry)? – Sati Nov 8 '20 at 13:05
  • Yes. Your function is not clean to me. A function's return value should be decided only by its arguments whenever it's possible. And a function's purpose should be as simple as possible. – xuchunyang Nov 8 '20 at 14:03
  • Other than what I pointed out above, can you show me how to make the function more concise? – Sati Nov 8 '20 at 15:06
  • I don't not know the purpose. If you want to extract year from something like 2020/11/09, you can create (defun extract-year (date-string) ...) which can be used as (extract-year "2020/11/09") => 2020 – xuchunyang Nov 8 '20 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.