Combining digits to make a string

Discussion of Common Lisp
duncan
Posts: 31
Joined: Wed May 27, 2009 9:07 pm

Re: Combining digits to make a string

Post by duncan » Fri May 29, 2009 9:54 pm

Actually, if I were an instructor I'd take a format-based solution if I were convinced it were original, but.. I'd sure want to know how the student came up with it. Well, maybe that's why I'm not an instructor.
smithzv wrote:
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.
The way I read it he just wants a string of three characters, each of which is either 0, 1, 2, or 3. So the base, leading zeros, etc. aren't relevant. There a lot of ways to do this in CL, but I'm surprised no one has mentioned DIGIT-CHAR.

This is pretty clearly homework, so I wouldn't normally give a full answer, but since there have been a couple of pretty complete answers so far here's a simple, but not very elegant solution:

(concatenate 'string (mapcar (lambda (x) (digit-char (random x))) '(4 4 4)))

Or at least I think that is correct- haven't really tested it or thought very hard about it so caveat lector.

JamesF
Posts: 98
Joined: Thu Jul 10, 2008 7:14 pm

Re: Combining digits to make a string

Post by JamesF » Sun May 31, 2009 4:58 pm

If you want to get baroque about it, you could try this:

Code: Select all

(parse-integer
  (format nil "~d~d~d"
    (list (random 3) (random 3) (random 3))))

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

Re: Combining digits to make a string

Post by miningold » Mon Jun 01, 2009 11:46 pm

Maybe if I explain why I want these numbers (or strings I'm not sure what to call them) it might clear up the confusion.

Like I said I am new at this, but I want to make a GA (genetic algorithm) using a representation of DNA instead of using binary for my population.
I was wondering what is the best way to achieve a strand of "DNA" to use in a GA.

I was thinking about these options:

((013) (201)...)
((0 1 3) (2 0 1)...)
or an array (which I do not know how to make)

Note: Duncan is right (I think) the leading zeros should not matter (but maybe they do), and no, this is not homework this is for my entertainment only.

Thank you for all of your responses.

-Miningold

duncan
Posts: 31
Joined: Wed May 27, 2009 9:07 pm

Re: Combining digits to make a string

Post by duncan » Tue Jun 02, 2009 12:38 am

miningold wrote: I was wondering what is the best way to achieve a strand of "DNA" to use in a GA.

I was thinking about these options:

((013) (201)...)
((0 1 3) (2 0 1)...)
or an array (which I do not know how to make)

Note: Duncan is right (I think) the leading zeros should not matter (but maybe they do), and no, this is not homework this is for my entertainment only.
OK- sorry for assuming it was homework. It really did look like homework ;). I realized I was wrong about that after seeing your other thread. You might do yourself a favor and preface future questions like this with: "THIS IS NOT HOMEWORK" ;). 'Cause there are still a few schools that use CL for some classes and they tend to assign their students exactly this kind of "getting used to lisp" thing. Some percentage of students decide to get clever and hit the forums- I think if you look through the archives of this forum you'll find a number of people with one or two posts who just wanted help with their homework.

Anyway, you really need to decide how you want to represent your data. There's a big difference, in CL, between "334" and ' (334) and '(3 3 4). Each can be converted to the other, if necessary, but... better to start with one format firmly in mind, and then change it if necessary. Of those three the '(( 3 3 4) (1 0 2)) form is the most flexible, and the easiest to process, IMHO. But this depends (obviously) on what you want to do with the data... that would be a poor way to store the data if it had to be interpreted numerically.

kroger
Posts: 12
Joined: Mon Jul 28, 2008 2:38 am

Re: Combining digits to make a string

Post by kroger » Tue Jun 02, 2009 12:41 am

miningold wrote: I was thinking about these options:

((013) (201)...)
((0 1 3) (2 0 1)...)
or an array (which I do not know how to make)
you can use an array (or a vector):

(vector 0 1 3) => #(0 1 3)

see more about it here:
http://gigamonkeys.com/book/collections.html

Pedro

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

Re: Combining digits to make a string

Post by miningold » Tue Jun 02, 2009 5:04 pm

I decided to use this code:

Code: Select all

(setq number-seeds 10)
(let (value)
  (dotimes (num number-seeds value)
    (setq value (cons (list (random 3) (random 3) (random 3))
		      value))))
I get a list of:
((. . .) (. . .) ...)

Is there a way to turn the list into a vector such as:
[(. . .) (. . .) ...]

or a different way to write the code so that i get a vector as shown above?

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

Re: Combining digits to make a string

Post by gugamilare » Tue Jun 02, 2009 5:24 pm

miningold wrote:I decided to use this code:

Code: Select all

(setq number-seeds 10)
(let (value)
  (dotimes (num number-seeds value)
    (setq value (cons (list (random 3) (random 3) (random 3))
		      value))))
I get a list of:
((. . .) (. . .) ...)

Is there a way to turn the list into a vector such as:
[(. . .) (. . .) ...]
Yes:

Code: Select all

(make-array (length some-list) :initial-contents some-list)
You can put NUMBER-SEEDS instead of (length some-list).
or a different way to write the code so that i get a vector as shown above?
Yes, this is simple to do as well:

Code: Select all

(let ((array (make-array number-seeds)))
  (dotimes (num number-seeds value)
    (setf (aref array num) (list (random 3) (random 3) (random 3)))))

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

Re: Combining digits to make a string

Post by miningold » Wed Jun 03, 2009 5:35 pm

gugamilare wrote:

Code: Select all

(make-array (length some-list) :initial-contents some-list)

Code: Select all

(let ((array (make-array number-seeds)))
  (dotimes (num number-seeds value)
    (setf (aref array num) (list (random 3) (random 3) (random 3)))))
I could not get either of those options working, but i think that it was my fault, as I have already said I am new to lisp

So would you mind giving me the code in the complete form not just additions as well as explanations so that i know what is going on.

Thank you

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

Re: Combining digits to make a string

Post by gugamilare » Wed Jun 03, 2009 6:19 pm

Using the first option - transforming a list into an array:

Code: Select all

(let ((number-seeds 10) value)
  (dotimes (num number-seeds value)
    (setq value (cons (list (random 4) (random 4) (random 4))
                      value)))
  (make-array number-seeds :initial-contents value))
Using the second option - constructing the array directly:

Code: Select all

(let* ((number-seeds 10)
       (array (make-array number-seeds)))
  (dotimes (num number-seeds array)
    (setf (aref array num) (list (random 4) (random 4) (random 4)))))
There was a bug in the second portion of the code, that's why you had trouble using it :)

EDIT: Oops! My bad. Should be (random 4) instead of (random 3), or else the number 3 will never come out. Now it is fixed.

Post Reply