Beginner question

Discussion of Common Lisp
Post Reply
cesar2000
Posts: 2
Joined: Sat Aug 30, 2008 5:40 am

Beginner question

Post by cesar2000 » Sat Aug 30, 2008 7:09 am

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

Post by cesar2000 » Sat Aug 30, 2008 7:49 am

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

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

Re: Beginner question

Post by Kohath » Sun Aug 31, 2008 9:14 pm

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.

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

Re: Beginner question

Post by Jasper » Fri Oct 10, 2008 8:56 am

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

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

Re: Beginner question

Post by qbg » Fri Oct 10, 2008 10:41 am

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

Post Reply