Loading ...
Sorry, an error occurred while loading the content.

704Generating codes/values

Expand Messages
  • oliver@first.in-berlin.de
    Jan 3, 2003
    • 0 Attachment

      I want to write tables of values/codes.
      It is given the number of digits of a code and
      the number of possible values for each
      digit (base of the code).

      And I want to know, how the problem can be solved
      elegant and efficient.

      let create_code len base = (* ... best approach ... *)

      create_code 3 2;; (* will be able to create this list: *)
      [ "000"; "001"; "010"; "011"; "100"; "101"; "110"; "111" ]

      But it is also intended to have the possibility of creating
      a gray-code:

      create_gray_code 3 2;; (* will be able to create this list: *)
      [ "000"; "001"; "011"; "010"; "110"; "111"; "101"; "100"]

      I thought about creating a list for each digit
      with a for-loop and the "::"-operator, to create
      the list for one digit out of strings (and the strings out of int's).

      Then I thought about using List.map2 with (^) to put the
      created lists of digit's together. I have to use this approach
      (len - 1) times (for three digits, as in the example
      above, I have to use it twice.)

      How would you solve this problem of code-generation?

      How can the type-system help in solving such a problem?
      Or is it a good idea to do it OO-like?

      What about codes with more than base 10 (e.g. hexcode)?
      And what's about creating lists of non-strings (e.g.
      lists of functions or objects, so that calling these
      objects/functions will create the intended result (e.g.
      printing LaTeX-code, or doing other actions))?

    • Show all 11 messages in this topic