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.
Beginner question
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))
(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))
-
- Posts: 61
- Joined: Mon Jul 07, 2008 8:06 pm
- Location: Toowoomba, Queensland, Australia
- Contact:
Re: Beginner question
I remember some comp.lang.lisp sage mentioning floor (http://www.lispworks.com/documentation/ ... floorc.htm). Thus directed, the definition of month could be:
Seeing that you now know about floor, the definition of year becomes even simpler than the above definition of month.
Code: Select all
(defun month (n)
(mod (floor n 100) 100))
Re: Beginner question
Are you sure they don't mean the string "yyyymmdd" then you can use subseq.
Of course, it assumes the string is long enough.
Hmm, is there an read-integer-from-string and such? (year "'hax0519") -> 'HAX ; 4
Code: Select all
(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)))
Hmm, is there an read-integer-from-string and such? (year "'hax0519") -> 'HAX ; 4
Re: Beginner question
Function PARSE-INTEGERJasper wrote:]
Hmm, is there an read-integer-from-string and such? (year "'hax0519") -> 'HAX ; 4