Gray Code Sequence in Scheme

Page 1 of 1

0 Replies - 2469 Views - Last Post: 08 November 2011 - 10:45 AM

#1 BBird40

Reputation: 0
• Posts: 30
• Joined: 09-February 11

Gray Code Sequence in Scheme

Posted 08 November 2011 - 10:45 AM

Hey guys, I am having to write the gray code sequence in scheme but I am running into trouble. When I have a list of just integers I cannot implement the way I want. When I do it as strings, I feel as if I can manipulate it like I want but I get errors.

This is what I have so far:

```(define (graycode n)
(let ((e1 "0"))
(let ((e2 "0"))
(let ((e3 "1"))
(let ((e4 "1"))
(cond((equal? n 0) (list '()))
((equal? n 1) (list 0 1))
(else((graycode (- n 1) (string-append e1 "0")(string-append e2 "1")(string-append e3 "0")(string-append e1 "1") (list '(e1 e2 e3 e4)))))))))))
```

Just know I have tried this multiple ways, with 2 lists, with one list. Now I am just trying strings and then putting them in a list once they are changed. If I wasnt at such a loss, I wouldn't have posted here. Honestly though, I do no know what to do next.

Here is the best way I have researched to obtain the gray code sequence: The base case is 1 bit, which results in ( 0 1). To go to n = 2, reflect the bits (it becomes (1 0)), concatenate that list with the original list to get (0 1 1 0) . Then prepend 0 to the original list and the 1 to the reflected list, resulting in ( 00 01 11 10).

However, I figured instead of just prepending you can append then put that value onto a list. This recursive suggestion was given to me by a good mentor of mine at my college.

What do you think?

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }