Add tags

Add tags to the following comment

僕もhaskellの練習で作ってみました。長くなってしまった。乱数の使い方が難しくって強引な事(unsafeを利用している)をしたけど、mattanさんがやってるようなすっきりとした利用方法があるんですね。

>writeRW2D "file.name" 100

時刻0-99までのxy位置が書かれる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import System.IO.Unsafe 
import System.Random

type XYPlot a = (a,a)

random4:: IO Int
random4 = getStdRandom (randomR (0,3))

nextStep::  XYPlot Int -> IO Int -> XYPlot Int 
nextStep (x,y) iorand 
    | rand == 0 = (x+1,y)
    | rand == 1 = (x,y+1)
    | rand == 2 = (x-1,y)
    | rand == 3 = (x,y-1)
    where rand =  unsafePerformIO $ iorand

randomWalk2D:: XYPlot Int -> [XYPlot Int]
randomWalk2D (x,y) = [(x,y)]++randomWalk2D (nextStep (x,y) random4)

formatRW2D:: XYPlot Int -> String
formatRW2D (x,y) = (show x) ++ " " ++ (show y) ++ "\n"

rw2DtoStr:: Int -> [XYPlot Int]->String
rw2DtoStr count (x:xs) = show count ++ " " ++
                         formatRW2D x ++ (rw2DtoStr (1+ count) xs)
rw2DtoStr count [] = ""

writeRW2D::FilePath -> Int ->IO()
writeRW2D fname terminal =      
    writeFile fname $rw2DtoStr 0 $take terminal $randomWalk2D (0,0)

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...