; compose : ('b -> 'c) ('a -> 'b) -> ('a -> 'c) ; sequence the functions a and b, returning the result of applying ; a to the result of applying b to x ; E.g. ((compose string->list number->string) 4) = (#\4) (define (compose a b) (lambda (x) (a (b x)))) ; string->byte-strings : string -> list-of-strings ; chop the string s into pieces of length 8 ; E.g. (string->byte-strings "0000000011111111") = (list "00000000" "11111111") (define (string->byte-strings s) (string-chop s 8)) ; string-chop : string non-negative-integer -> list-of-strings ; chop the string s into pieces of length length-of-pieces (define (string-chop s length-of-pieces) (let ([len (string-length s)]) (do ([i 0 (+ i length-of-pieces)] [bs '() (cons (substring s i (min (+ i length-of-pieces) len)) bs)]) [(>= i len) (reverse bs)]))) ; binary-string->integer : string-of-zeros-and-ones -> integer ; converts a string of binary digits into an integer ; E.g. (binary-string->integer "101") = 5 (define (binary-string->integer b) (string->number b 2)) ; decode : string -> string ; converts a message written in binary to a normal string (define (decode message) (apply string (map (compose integer->char binary-string->integer) (string->byte-strings message)))) ;; The following message appeared on a ThinkGeek tshirt ;; on the first of april 2004. (define thinkgeek-tshirt-message (string-append "010010010010000001110011011010000110111" "101110000011100000110010101100100001000" "000110000101110100001000000101010001101" "000011010010110111001101011010001110110" "010101100101011010110010000001101111011" "011100010000001000001011100000111001001" "101001011011000010000001000110011011110" "110111101101100011100110010000001000100" "011000010111100100101100001000000110000" "101101110011001000010000001100001011011" "000110110000100000010010010010000001100" "111011011110111010000100000011101110110" "000101110011001000000111010001101000011" "010010111001100100000011011000110111101" "110101011100110111100100100000011100110" "110100001101001011100100111010000100001")) (decode thinkgeek-tshirt-message)
string->number and number->string to convert between a string containing the external (i.e. printed) representation of a number and number itself. For example:
> (string->number "100") 100 > (number->string 100) "100"
binary-string->integer with the procedure:
; hexadecimal-string->integer : string-of-0-to-9-and-A-to-F -> integer ; converts a string of binary digits into an integer ; E.g. (hexadecimal-string->integer "A9FF") = 43519 (define (hexadecimal-string->integer b) (string->number b 16))
| CookbookForm | |
|---|---|
| TopicType: | Recipe |
| ParentTopic: | StringRecipes |
| Other Parents: | |
| Next Topic: | StringCSV |