Page 1 of 1

Beginner question

Posted: Sat Aug 30, 2008 7:09 am
by cesar2000
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

Posted: Sat Aug 30, 2008 7:49 am
by cesar2000
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

Posted: Sun Aug 31, 2008 9:14 pm
by Kohath
I remember some comp.lang.lisp sage mentioning floor (http://www.lispworks.com/documentation/ ... floorc.htm). Thus directed, the definition of month could be:

Code: Select all

(defun month (n)
  (mod (floor n 100) 100))
Seeing that you now know about floor, the definition of year becomes even simpler than the above definition of month.

Re: Beginner question

Posted: Fri Oct 10, 2008 8:56 am
by Jasper
Are you sure they don't mean the string "yyyymmdd" then you can use subseq.

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)))
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

Posted: Fri Oct 10, 2008 10:41 am
by qbg
Jasper wrote:]
Hmm, is there an read-integer-from-string and such? (year "'hax0519") -> 'HAX ; 4
Function PARSE-INTEGER