Convert Python to Lisp

Convert Python to Lisp

phmzanatta93
Explorer Explorer
801 Views
4 Replies
Message 1 of 5

Convert Python to Lisp

phmzanatta93
Explorer
Explorer

Hi everyone, 

 

I am currently on the process of learning Lisp coding. I am trying to do an exercise and it is basically this python code:

 

import random target_num, guess_num = random.randint(1, 10), 0 while target_num != guess_num: guess_num = int(input('Guess a number between 1 and 10 until you get it right : ')) print('Well guessed!')

 

How would I write thin in Lisp to run it on AutoCAD?

0 Likes
Accepted solutions (1)
802 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

There is no such function as "random" in lisp. Has to be a custom func - as THIS  example.

0 Likes
Message 3 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Heres your one.

 

(defun c:GuessNumber ( / LM:rand LM:randrange taget_number guess_num)
  
  ;; Rand  -  Lee Mac
  ;; PRNG implementing a linear congruential generator with
  ;; parameters derived from the book 'Numerical Recipes'
  
  (defun LM:rand ( / a c m )
    (setq m   4294967296.0
	  a   1664525.0
	  c   1013904223.0
	  $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
	  )
    (/ $xn m))
  
  (defun LM:randrange ( a b )
    (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b)))))))
  
  ; ----------------------------------
    
  (setq taget_number (LM:randrange 1 10))
  
  (while (/= taget_number guess_num)
    (setq guess_num (getint "\nGuess a number between 1 and 10 until you get it right: ")))
  
  (princ "\nFinally! Now you're right.")
  (princ)
  )

 

Message 4 of 5

phmzanatta93
Explorer
Explorer

This worked! It considers 0 as a possible answer as well though. Thanks a lot for your help and I realized there will be a long journey until I get hold of this. 

 

Thanks!

0 Likes
Message 5 of 5

ВeekeeCZ
Consultant
Consultant

I don't know python much. So if that code restricts a user input to a range 1-10, that is what lisp can't. has to be done manually. Just THIS is possible.

0 Likes