Discuss and learn Lisp programming of all dialects
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.
I'm new to Lisp, using lispworks 6.1 personal on windows seven.
This is a function which will eventually have calDa as its argument.
All the variables should do like 31 bit integer arithmetic like truncated divide.
Lispworks says calDa unbound.
(defun linDaOfCalDa ()
(declare ((signed-byte 32) linDaOfCalDa calDa yr mo da i r mo14by12 calDaOfLinDa linDa) )
(set calDa 20140920)
(set mo (/ calDa 100))
(set yr (/ calDa 10000))
(set da (- calDa (* mo 100)))
(mo (- mo (* yr 100)))
(mo14by12 (/ (- mo 14) 12))
(i (+ yr 4900 mo14by12))
(r (+ (* (1461 (/ i 4))) 32075) da)
(r (+ r (* 367 (- mo (- 2 (* mo14by12 12))))))
(r (- r (/ (* 3 (/ (+ yr 4900 mo14by12) 100)) 4)))
(linDa (- r 1721060))
return(linDa)
)
Last edited by nuntius on , edited 1 time in total. Reason: added [code][/code] tags
Because set evaluates its first argument and it's an unbound symbol calDa.
Look at this:
(defvar a nil)
(defvar b (find-symbol "A"))
(set b t)
a ; => t
so you are looking for devfar & setq (or setf) or let, just check them all out.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
Thank you for teaching me. I modified the function.
Why is my answer now an unfinished divide?
And I am still wondering about how to ensure integer arithmetic.
CL-USER 1 > (defun linDaOfCalDa() ;calDa to be future function parameter
(declare ((signed-byte 32) linDaOfCalDa calDa yr mo da i r mo14by12 calDaOfLinDa linDa) )
(setf calDa 20140921) ;this line to be omitted later
(setf mo (/ calDa 100))
(setf yr (/ calDa 10000))
(setf da (- calDa (* mo 100)))
(setf mo (- mo (* yr 100)))
(setf mo14by12 (/ (- mo 14) 12))
(setf i (+ yr 4900 mo14by12))
(setf r (+ (/ (* 1461 i) 4) -32075 da))
(setf r (+ r (* 367 (- mo (- 2 (* mo14by12 12))))))
(setf r (- r (/ (* 3 (/ (+ yr 4900 mo14by12) 100)) 4)))
(setf linDa (- r 1721060))
(setf linDaOfCalDa linDa)
)
LINDAOFCALDA
For the integer division you should look into the rounding family of functions, you'll probably pick the floor function + mod & rem.
The declare is only for optimization, not for changing the meaning of a program.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
More examples can be found in Practical Common Lisp, Chapter 10, Section Numbers plus the following few sections, demonstrating Common Lisp math using various types of numbers.
I hope yhat I got one small step ahead,
but it says that INTDIV is unbound.
(defun intDiv(num den)
(setf ans (truncate num den))
(print ans)
(return ans)
)
(print intDiv(-3 2))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
You shouldn't use return in CL. The defun has an implicit progn as a body and its value is a value of the last expression so you just write:
(defun intDiv (num den)
(truncate num den))
or
(defun intDiv (num den)
(let ((ans (truncate num den)))
ans))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
I used you're model and tried several calls following it, and they were correct:
CL-USER 1 > (defun intDiv (num den)
(let ((ans (truncate num den)))
ans))
INTDIV
CL-USER 2 > (print (intDiv -3 2))
-1
-1
CL-USER 3 > (print (intDiv -2 2))
-1
-1
CL-USER 4 >
Why do I get two prints from Lispworks for each call?
print returns its first argument and your Lispworks is running REPL. When you want test your function just call it in the REPL and don't use print, it's redundant.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
(setf linDa 735868) ;linDa of 20140927 is 735868
(print linDa)
;int r=linDa+1721060+68569;
(setf r (+ linDa 1721060 68569))
(print r)
;int f=4*r/146097; //if both are integer type, quotient is integer
(setf f (truncate (* 4 r) 146097))
(print f)
;r=r-(146097*f+3)/4;
(setf r (- r (truncate (+ (* 146097 f) 3) 4)))
(print r)
;int y=4000*(r+1)/1461001;
(setf y (truncate (* 4000 (+ r 1)) 1461001))
(print y)
;r=r-(1461*y/4-31);
(setf r (- r (- (truncate (* 1461 y) 4) 31)))
(print r)
;int m=80*r/2447;
(setf m (truncate (* 80 r) 2447))
(print m)
;int da=r-2447*m/80;
(setf da (- r (truncate (* 2447 m) 80)))
(print da)
;r=m/11;
(setf r (truncate m 11))
(print r)
;int mo=m+(2-12*r);
(setf mo (+ m (- 2 (* 12 r))))
(print mo)
;int yr=100*(f-49)+y+r;
(setf yr (+ (* 100 (- f 49)) y r)
(print yr)
;int calDa=yr*10000+mo*100+da;
(setf calDa (+ (* yr 10000) (* mo 100) da))
(print calDa)
;return(calDa)
I am trying to translate some simple java code to Lisp.
I place the exact above code into "LispWorks 6.1 Personal" and
and yr and calDa do not print (why?) but the previous ones print and
agree exactly with a similar version of my java code which I know
is correct because it's been extensively tested.