Reputation: 1157
I'm quite new to scheme; I'm more familiar with python, for example. However, I'm trying to learn, because scheme seems like a very interesting language.
I'm trying to create some code that
Create a program that prints all whole numbers inclusively between an interval (a, b), and replaces every multiple of 8 in the sequence with a random (uniformly distributed), non-numeric, non-whitespace, printable ASCII character. Assume a < b in all cases. If the number has more than 1 digits, make sure the amount of characters in the replacement matches!
It was originally so I could try to answer the question here (a code golf question) but now I'm just trying to do it because I don't know how, so I'm not worrying about the length of the solution. Right now, I'm not doing anything with the ascii characters bit, just trying to get something working. I'm coding it online, in repl.it, and there isn't any output, not even an error. My expected output would be it running the function for the interval 1 through 16 and printing the sequence 1 2 3 4 5 6 7 h 9 10 11 12 13 14 15 h
.
(define a 1)
(define b 16)
(define (s8 a b)
(let loop ((a<=b))
(if (= (mod a 8) (0))
(write h))
(else
(write a))
(define a (+ a 1))
(s8 a b)
(write "hello")
Any help would be appreciated. Thanks!
Upvotes: 1
Views: 67
Reputation: 286
I'm not specifically a schemer, but I'm pretty familiar with the other lisps and from your code I can imagine how confused you are. I patched up your code a little so it runs, although I avoided changing very much so the output probably isn't exactly what you're expecting.
(define a 1)
(define b 16)
(define (s8 a b)
(when (<= a b)
(if (= (mod a 8) 0)
(write "h")
(write a))
(s8 (+ a 1) b)))
(s8 a b)
(write "Hello")
Firstly, in lisps, if always has an else case- it takes three expressions, the first is the test, the second is the "then" branch, the third is the "else" branch. "when" and "unless" let you have multiple lines of code sequentially, but you lose the "then" or "else" cases respectively.
Second, in scheme, you do looping with recursion. Afaik, in basic scheme this is the only kind of looping, but they have a lot of tricks to optimize it.
Upvotes: 2