Lab Assignment 3

  1. [solve] Write a definition of function attachToEnd(s,c), which produces the string that you get by adding character c to the end of string s. For example,

      attachToEnd("abcd", 'e') = "abcde"
      attachToEnd("giraffe", 's') = "giraffes"
    
    Keep in mind that s is a string but c is a character. Use the $ function to convert the character to a string and the ++ operator to concatenate strings.

  2. [solve] Write a definition of function rotate(s), which produces the string that you get by moving the last character of string s to the beginning of s. Assume that s is not an empty string. For example,

      rotate("abcd") = "dabc"
      rotate("rabbit") = "trabbi"
      rotate("aabbcc") = "caabbc"
      rotate("a") = "a"
    

  3. [solve] Write a definition of function removeFirst(n, s), where n is a nonnegative integer and s is a string. It should yield the string that you get be removing the first n characters from string s. (If s has fewer than n characters, then removeFirst(n, s) should yield an empty string.)

      removeFirst(0, "rabbit") = "rabbit"
      removeFirst(1, "rabbit") = "abbit"
      removeFirst(2, "rabbit") = "bbit"
      removeFirst(3, "rabbit") = "bit"
      removeFirst(4, "rabbit") = "it"
      removeFirst(5, "rabbit") = "t"
      removeFirst(6, "rabbit") = ""
      removeFirst(7, "rabbit") = ""
      removeFirst(2, "abcd") = "cd"
      removeFirst(5, "") = ""
    

  4. [solve] Write a definition of function stutter(s), which produces the string that you get by repeating each character in string s twice. For example,

      stutter("abcd") = "aabbccdd"
      stutter("giraffe") = "ggiirraaffffee"
      stutter("y") = "yy"
      stutter("") = ""
    

    Hint.

    1. Have a case for stutter("").

    2. When s is not an empty string, just concatenate the first character of s, then the same thing again, then stutter(tail(s)). Remember that you need to convert a character to a string to be able to use operator ++ with it.

  5. [solve] Write a definition of function binary(n) that takes a positive integer n and yields a string that is the base 2 (binary) representation of n. For example, 310 = 112, so binary(3) = "11". Here are some more examples.

      binary(1) = "1"
      binary(4) = "100"
      binary(6) = "110"
      binary(9) = "1001"
    

    Hint.

    1. Have a case for binary(1).

    2. If n is even then the binary representation of n ends on 0. Get binary(n `div` 2) and add a 0 to the end. For example,

        binary(6) = binary(3) ++ "0" = 110
      

    3. If n is odd then the binary representation of n ends on 1. Get binary(n `div` 2) and add a 1 to the end.

  6. [solve] Write a definition of function shuffle(s, t) that takes two strings s and t and produces a string that shuffles the two strings together, character by character, starting with the first character of s, then the first character of t, then the second character of s, then the second character of t, etc. When one of the strings runs out, just finish with all of the remaining characters in the other string. For example,

      shuffle("xxxx", "bbbb") = "xbxbxbxb"
      shuffle("abc", "xyz") = "axbycz"
      shuffle("abcdefg", "xyz") = "axbyczdefg"
      shuffle("", "mouse") = "mouse"
      shuffle("horse", "") = "horse"
    
    Think about the cases that you will need. Start with easy cases.