This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Beginner question

5 posts · 4738 views

Hey I just started learning lisp, and there's a problem in my text-book I just can't seem to solve. Hope someone can help out.

We have a date in the form yyyymmdd. Define 3 functions year, month and day, which from a date will give the values year, month and day. e.g:

(year 19980220)
->1998

(month 19980220)
->2

(day 19980220)
->20

thanks.

Re: Beginner question

Ok I managed to figure out a way to solve it. Is there a better way?

(defun day (n) (mod n 100))
(defun month (n) (mod (/ (- n (day n)) 100) 100))
(defun year (n) (/ (- n (day n) (* (month n) 100)) 10000))

Re: Beginner question

Are you sure they don't mean the string "yyyymmdd" then you can use subseq.
(defun year(str) (read-from-string(subseq str 0 4)))
(defun month(str) (read-from-string(subseq str 4 6)))
(defun day(str)(read-from-string(subseq str 6 8)))
Of course, it assumes the string is long enough.
Hmm, is there an read-integer-from-string and such? (year "'hax0519") -> 'HAX ; 4

Re: Beginner question

Jasper wrote:]
Hmm, is there an read-integer-from-string and such? (year "'hax0519") -> 'HAX ; 4
Function PARSE-INTEGER