r/programming_jp • u/WhiteCat6142 関数型中級者 • Mar 13 '16
ズンドコキヨシ てくもさんはTwitterを使っています: "Javaの講義、試験が「自作関数を作り記述しなさい」って問題だったから 「ズン」「ドコ」のいずれかをランダムで出力し続けて「ズン」「ズン」「ズン」「ズン」「ドコ」の配列が出たら「キ・ヨ・シ!」って出力した後終了って関数作ったら満点で単位貰ってた"
https://twitter.com/kumiromilk/status/707437861881180160
15
Upvotes
5
5
u/starg2 Mar 13 '16
D で無駄に Generator を使って書いてみた
import std.algorithm;
import std.array : array;
import std.concurrency : Generator, yield;
import std.random : uniform;
import std.range : chain, repeat;
import std.stdio;
void main()
{
auto kiyoshi = new Generator!string(
{
// ゴミで埋めておく
int[] outputs = (-1).repeat(5).array;
do
{
auto newOutput = uniform!"[]"(0, 1);
yield(newOutput ? "ドコ" : "ズン");
outputs = outputs[1..$] ~ newOutput;
}
while (!outputs.equal(0.repeat(4).chain([1])));
yield("キ・ヨ・シ!");
}
);
kiyoshi.each!writeln;
}5
u/kagcc λ Mar 13 '16
例えばこんな感じかなあ.Haskell
import System.Random as R
data ZunDoko = Zun | Doko deriving (Enum, Eq)
instance Show ZunDoko where
show Zun = "ズン"
show Doko = "ドコ"
kiyoshi :: [ZunDoko] -> [ZunDoko]
-- 最後の「ドコ」の直前まで
kiyoshi l = map fst $ pre ++ takeWhile ((== Zun) . fst) post
where
(pre,post) = span ((<= 4) . snd) . zip l $ scanl f 0 l
f n Zun = succ n
f _ Doko = 0
main = do
g <- getStdGen
mapM_ (putStr . show) . kiyoshi . map toEnum . randomRs (0,1) $ g
putStr "ドコキ・ヨ・シ!"5
u/fydede Mar 13 '16
JavaScript(Chrome)
ちょっと凝ったことしようとしたら無駄に行数増えた
function async(a,b,h){var k=arguments,c=a.apply(h||this,b),f=function(d){return e(d,c["throw"].apply(c,k))},e=function(d,a){var g=a||c.next(d);if(!g.done)try{g.value(e,f)}catch(b){f(b)}};e()};
async.sleep = function(time){ return (resolve, reject) => setTimeout(resolve, time); };
function* range(n){ var i=0; while(i<n) yield i++; }
function randomChoice(arr){ return arr[ Math.floor(Math.random()*arr.length) ]; }
function isPreKiyoshi(zds){ return zds.join('-') == ['ズン', 'ズン', 'ズン', 'ズン', 'ドコ'].join('-'); }
var past = ['','','','',''];
var zunPhase = 0;
async(function*(){
for(const _ of range(100)) {
const zd = randomChoice(['ズン', 'ドコ']);
past.shift();
past.push(zd);
if(zd == 'ズン') {
console.log('ズン' + ' '.repeat(zunPhase));
yield async.sleep(zunPhase==0 ? 1000 : 500);
zunPhase++;
continue;
}
console.log('ドコ');
if( isPreKiyoshi(past) ) {
yield async.sleep(500);
console.log('キ・ヨ・シ!');
console.log('~終~');
return;
}
console.log('');
yield async.sleep(500);
zunPhase = 0;
}
console.log('no キヨシ');
});5
u/buhoho Mar 13 '16
再帰を使ってみた(javascript)
function zundoko(n) {
var i = Math.round(Math.random());
console.log(['ドコ', 'ズン'][i]);
if (i == 1)
return zundoko(n + 1);
if (n == 4)
return true;
return zundoko(0);
}
zundoko(0) && console.log("キ・ヨ・シ!");3
5
u/sifisifi Mar 13 '16
Haskell
module Main where
import Data.Conduit
import qualified Data.Conduit.List as CL
import System.Random (randomRIO)
main :: IO ()
main = do
let f (0,0,0,0,1) = return Nothing
f (a1,a2,a3,a4,a5) = randomRIO (0,1) >>= \n -> return $ Just (n, (a2,a3,a4,a5,n))
CL.unfoldM f (1,1,1,1,1) $$ CL.mapM (putStrLn . (["ズン", "ドコ"] !!)) =$ CL.sinkNull
putStrLn "キ・ヨ・シ!"conduit+馬鹿正直にパターン見る方法で何とか行数減らせないかなと結構考えた
それぞれ無理やり一行に収めてる気がしないでもない
4
u/kosh04 Mar 17 '16 edited Mar 29 '16
Common Lispで再帰
(defun zundoko (&optional (n 0))
(let ((s1 (elt #(#1=ズン #1# #1# #1# #2=ドコ #3=キ・ヨ・シ!) n))
(s2 (elt #(#1# #2#) (random 2))))
(cond ((eq s1 '#3#) s1)
((eq s1 s2) (cons s2 (zundoko (1+ n))))
(t (cons s2 (zundoko))))))
(print (zundoko))実行例: http://melpon.org/wandbox/permlink/G5yLmxMlcbg5sAEk
※再帰ミスでズンズンだけしか拾わなかった箇所を修正
3
4
u/baal2015 Mar 13 '16
日本語版のFizzBuzz問題として広めてほしい
schemeで
(import (scheme base)
(scheme lazy)
(srfi 27))
(define (stream-zundoko)
(let loop ((count 0))
(delay
(if (odd? (random-integer 2))
(cons "ズン" (loop (+ 1 count)))
(cons "ドコ"
(if (= count 4)
(delay (cons "キ・ヨ・シ!" (delay '())))
(loop 0)))))))
(let loop ((zundoko (stream-zundoko)))
(when (pair? (force zundoko))
(write-string (car (force zundoko)))
(loop (cdr (force zundoko)))))
6
u/WhiteCat6142 関数型中級者 Mar 13 '16
最近始めたgolangで