Dice Roller, Sentance Generator, and Morse Code Translator

October 3, 2009
by David Warshow (diw08)

Multiple Functions (programs) in one script integrated with the project script from code emersion.

type (s) to generate a sentance

type (d [sides on dice] [rolls to make])  to roll dice of any type

type (morse ‘[one letter]) converts to morse code  :: Example :: (morse ‘a)

I am working on improving the morse translator so it can do strings and not single chars. My eventual goal is to be able to call (morse (s)) to generate a sentence and immediately translate it into morse code.

Code here –>  project <–

Tags: , , ,



3 Responses to “Dice Roller, Sentance Generator, and Morse Code Translator”

  1.   asm09 Says:

    nifty syntactic form:

    instead of a string of (cond ((equal? … you can use the ‘case’ form:

    (case input
    (1 “Ready…”)
    (2 “Set…”)
    (3 “Go!…”))

    or somesuch. It looks for the value of ‘input’ in the list you provide and returns the corresponding expression. Only thing to watch out for is that it will use eqv?, not equal?, but I think that for symbols it works the same way you’re doing.

    🙂

  2.   lspector Says:

    Nice stuff. A couple of comments:

    – The morse code thing as posted will always return error because you start with:

    (lambda input

    whereas you really meant to start with:

    (lambda (input)

    What you did is legal but it means something different — it means that morse can be called with any number of arguments and that input will be bound to a list of all of those arguments…. so it will always be a list and never be equal to ‘a or any of the other things you compare it to.

    – You don’t need that num variable. Instead of this:

    ((equal? input ‘a)
    (set! num ‘(.-))
    num)

    you can just do this:

    ((equal? input ‘a)
    ‘(.-))

    – As Adria noted the “case” syntactic form can make this even cleaner, but in Scheme the first thing in each case clause should be a list of values to match, not just a single value to match. So you could do something like this:

    (define (morse)
    (lambda input
    (case input
    ((a) ‘(.-))
    ((b) ‘(-…))

    etc.

    To extend your morse code generator to work on strings you should first look into the character data type. You are currently operating on symbols that happen to have single-character names, but that’s not the same as operating on characters. Similar code will work, but it won’t be exactly the same. Once you have it working on characters you can loop down a string (a sequence of characters) to do what you want.

    -Lee

    P.S. I wish there was a way to get indenting to work correctly in comments… I’m going to look into that.

  3.   lspector Says:

    An alternative to my last suggestion: You could continue to work on symbols (that happen to have single-character names) and convert an input string into a list of such symbols:

    First use the string->list function to get from a string to a list of characters (the #\ syntax shows that these are characters). It works like this:

    > (string->list “astronomy”)
    (#\a #\s #\t #\r #\o #\n #\o #\m #\y)

    Then use make-string for each of these to produce a single-character string with the given character as its single character. It works like this:

    > (make-string 1 #\a)
    “a”

    Then use string->symbol to make a symbol out of each of these strings. It works like this:

    > (string->symbol “a”)
    a

    Now you can put this all together with some loops (here implemented using “map”) to write a function that takes a string as input and returns a list of symbols (one for each character of the string):

    [APOLOGIES FOR THE LACK OF INDENTATION HERE — copy into DrScheme, highlight, and hit tab to see the structure of the code.]

    (define string->symbol-list
    (lambda (str)
    (map string->symbol
    (map (lambda (char)
    (make-string 1 char))
    (string->list str)))))

    Then:

    > (string->symbol-list “astronomy”)
    (a s t r o n o m y)

Leave a Reply

You must be logged in to post a comment.