## Combining digits to make a string

Discussion of Common Lisp

### Combining digits to make a string

Warning: I am completely new to lisp, I started two days ago.

What I want to do is make a 3 digit number, randomly generated and each number can only range from 0 to 3

ex: '301' '123' '000' '333' ...

They way I was trying to do it was use the random funcion (random 3) then append all three of the digits however whenever I get it to work it makes a list not a string (ex: (0 1 2) or (0 0 0))

The code I used to get those results was:

Code: Select all
`(concatenate 'list (list (random 3)) (list (random 3))(list (random 3))) => (1 0 3)`

Yes I know that 'list makes it a list but everything else I tried did not work.

miningold

Posts: 5
Joined: Tue May 26, 2009 7:03 pm

### Re: Combining digits to make a string

333 = 3 * 100 + 3 * 10 + 3 * 1

Does that help?
Paul Donnelly

Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### Re: Combining digits to make a string

There is also quite silly solution which, if this is in fact homework of some sort, is not the answer you are looking for, but I just cannot resist:
Code: Select all
`(format nil "~4,3,'0r" (random 64))`
Ramarren

Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

### Re: Combining digits to make a string

Ramarren wrote:There is also quite silly solution which, if this is in fact homework of some sort, is not the answer you are looking for, but I just cannot resist:
Code: Select all
`(format nil "~4,3,'0r" (random 63))`

Nice thinking! Just use numbers in base 4. His teacher won't accept this answer, but that is a nice one.

I think you should put (random 64) since (random 63) will not include the number 63 itself (and 63 will generate the string "333").

Code: Select all
`(concatenate 'list (list (random 3)) (list (random 3))(list (random 3)))`

it is much simpler and efficient to write

Code: Select all
`(list (random 3) (random 3) (random 3))`

gugamilare

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

### Re: Combining digits to make a string

I believe you were implicitly suggesting, "why doesn't it work to do some thing like...
Code: Select all
`(coerce '(1 2 3) 'string)`

...which is basically equivalent to your...
Code: Select all
`(concatenate 'string '(1 2 3))`

?" After all, this works:
Code: Select all
`(coerce '(1 2 3) 'vector) ==> #(1 2 3)(concatenate 'string '(#\1 #\2 #\3)) ==> "123"`

While it is true that strings are nothing more than vectors made of characters, in Common Lisp (as in most languages) the number 1 and the character 1 (denoted #\1 in Common Lisp) are very distinct. So, if you want to pick random numbers and produce random character versions of those numbers, you need a map (or way to convert) between numbers and characters. Once you have such a map, you can MAPCAR over your list and feed the result to COERCE.

One thing to look at are the functions CHAR-CODE and CODE-CHAR. Another (IMO, cleaner) method is to look into writing a new random-like function that chooses random characters by some means instead of choosing random numbers.

Best,
Zach
smithzv

Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

### Re: Combining digits to make a string

Here are a few things that sprang to mind.

Code: Select all
`(defun miningold.alphabet (&key (length 3) (alphabet "0123"))  (let ((output (make-string length)))    (dotimes (i length output)      (setf (aref output i)            (aref alphabet (random (length alphabet)))))))(defun miningold.numbers (&key (length 3) (limit 3))  (let ((output (make-string length)))    (dotimes (i length output)      (setf (aref output i)            (digit-char (random limit))))))`
xach

Posts: 6
Joined: Tue Nov 11, 2008 7:48 am

### Re: Combining digits to make a string

smithzv wrote:I believe you were implicitly suggesting, "why doesn't it work to do some thing like...
Code: Select all
`(coerce '(1 2 3) 'string)`

...which is basically equivalent to your...
Code: Select all
`(concatenate 'string '(1 2 3))`

?" After all, this works:
Code: Select all
`(coerce '(1 2 3) 'vector) ==> #(1 2 3)(concatenate 'string '(#\1 #\2 #\3)) ==> "123"`

Wait, does "miningold" want a number or a string? Because I thought he wanted a number:

What I want to do is make a 3 digit number, randomly generated [...]

but you are teaching him how to obtain a string instead.
gugamilare

Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

### Re: Combining digits to make a string

If you want string representation:
(concatenate 'string (princ-to-string (random 3)) (princ-to-string (random 3)) (princ-to-string (random 3)))
->"112"
Else I don't know how to present for example 012 as number as starting zeros will be removed.
012
->12

bobi
slobodan.blazeski

Posts: 9
Joined: Tue May 26, 2009 8:27 am

### Re: Combining digits to make a string

gugamilare wrote:Wait, does "miningold" want a number or a string?

According to the post title, a string, and according to the post body, a number.
Paul Donnelly

Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

### Re: Combining digits to make a string

Paul Donnelly wrote:
gugamilare wrote:Wait, does "miningold" want a number or a string?

According to the post title, a string, and according to the post body, a number.

Yeah, I took his example of '000' in the body to mean he wants to deal with strings. Just because number has a specific meaning in CL doesn't mean I might not refer to a number that is contained in a string. Also, the question of how many digits are in a number is kind of ill formed until you decide on a few details regarding how a number is printed. For instance, whether leading zeros should be printed or what base the number should be printed in.

Zach S
smithzv

Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Next