Beginner question

Discussion of Common Lisp

Beginner question

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.
cesar2000

Posts: 2
Joined: Sat Aug 30, 2008 5:40 am

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

Posts: 2
Joined: Sat Aug 30, 2008 5:40 am

Re: Beginner question

I remember some comp.lang.lisp sage mentioning floor (http://www.lispworks.com/documentation/HyperSpec/Body/f_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.

Kohath

Posts: 61
Joined: Mon Jul 07, 2008 8:06 pm
Location: Toowoomba, Queensland, Australia

Re: Beginner question

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
Jasper

Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands

Re: Beginner question

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

Function PARSE-INTEGER
qbg

Posts: 64
Joined: Mon Jun 30, 2008 1:05 pm
Location: Minnesota