2016-12-24 26 views
1

スキームにランダムなASCII文字(大文字または小文字)または文字(ただし数字ではない)を生成する必要があり、それを行うための適切な方法が何であるか不思議でした。現在、私は動作するコードランダムなASCII文字を生成する

(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) 

(エラーなし)を持っているが、代わりにランダムなASCII文字/文字を印刷する私はそれを行う方法を知らなかったので、私は、「h」を取得します。私は周りを見つけたが、何かを見つけることができなかった。どんな助けもありがとう。ありがとう!

EDIT:

(define random-char 
    (let* ((chars '("a" "e" "i" "o" "u")) 
     (len (length chars))) 
    (lambda() 
     (list-ref chars (random len))))) 
(define a 1) 
(define b 16) 
(define (s8 a b) 
    (when (<= a b) 
    (if (= (mod a 8) 0) 
     (random-char) 
     (write a)) 
    (s8 (+ a 1) b))) 
(s8 a b) 

がエラーに

1234567 
Error: execute: unbound symbol: "random" [s8, s8, s8, s8, s8, s8, s8, s8, random-char] 
+0

ホイールを改造しないでください! Schemeには、ランダムな値を生成するための組み込みプロシージャがあります。 –

答えて

1

を与える簡単な方法は、リスト内のすべての許容文字を配置し、ランダムにから1を選択することです:

(define random-letter 
     ; this is to avoid redefining the list of acceptable letters every time 
    (let* ((chars '("a" "e" "i" "o" "u")) 
     ; and this is to pre-calculate the length 
     (len (length chars))) 
    (lambda() ; the actual procedure, it doesn't require arguments 
     (list-ref chars (random len))))) ; pick a random index from list 

メイクリストに必要な文字列をすべて追加してください。この手順は次のように簡単です:

(random-letter) 
=> "u" 
+0

私は定義を最初に置き、 '(write" h ")'の代わりに '(random-letter)'に入れ、 '1234567 エラー:実行:アンバウンドシンボル:" random "[s8、 s8、s8、s8、s8、s8、s8、s8、random-char] 'を指定します。 – heather

+0

これは、あなたのインタプリタが 'random'手続きを持っていないか、別の名前が付けられていることを意味します。あなたはどの通訳を使っていますか?マニュアルを参照して、正しい手順を見つけてください。または、ラケットをダウンロードしてください。ラケットは、学習に本当に便利です。また、コードを実行しているとわからないので、表示された出力が意味をなさない - すべてをクリアし、上記のスニペットをコピーして貼り付けてください。 –

+0

私はオンラインインタープリタrepl.itを使用しています。 「BiwaScheme Interpreter version 0.6.4 Copyright(C)2007-2014 HARAとBiwaSchemeチーム」と書かれています。私は実行したコードを編集しました。 – heather

0

これは、業界をリードするScheme派生言語、特にRacketでこれを行う方法です。文字と整数を変換する関数 、文字と整数を変換する関数、PRNG (カレーと同様)のような、もっとベアボーンのスキームで実装する必要があるかもしれないさまざまな機能を前提としています。

スキームにこの機能の一部がない場合は、教育的に面白いかもしれないが、何も書かなければならないかもしれません。

(define latin-alpha-string 
    ;; This assumes that a-z are a sequence of adjacent, increasing 
    ;; character codes, as are A-Z, but nothing about their relative values 
    (let ([lca (char->integer #\a)] 
     (uca (char->integer #\A))) 
    ;; build-string makes a string by calling a function which takes an index 
    ;; and returns the character at that index. 
    (build-string (+ 26 26) 
        (λ (i) 
        (integer->char 
        (if (< i 26) 
         (+ lca i) 
         (+ uca (- i 26)))))))) 

(define (random-string-char s (rand random)) 
    ;; The general case: return a random element of a string. rand, if provided, 
    ;; should be a function of one argument, i, which returns a (pseudo-)random 
    ;; integer in [0, i). 
    (string-ref s (rand (string-length s)))) 

(define random-latin-alpha-char 
    ;; and here's a curried version of the above for latin-alpha strings 
    (curry random-string-char latin-alpha-string)) 
関連する問題