トップ
新規
単語検索
ヘルプ
ネタ記録庫/AAGraph
をテンプレートにして作成
開始行:
[[ネタ記録庫/AAGraph]]
#contents
* [[AA折れ線グラフ:http://oss.timedia.co.jp/index.fcgi/ka...
>AAで折れ線グラフを書くというお題.
>入力は'R','F','C'の3種類も文字からなる長さ1以上の文字列
>'R'は上昇を表し,折れ線グラフの要素としては '/' (スラッ...
>'F'は下降を表し,折れ線グラフの要素としては '?' (バック...
>'C'は変化なしを表し,折れ線グラフの要素としては'_'(アン...
>たとえば,
$ ./plot RCRFCRFFCCRFFRRCRRCCFRFRFF
>とすると
__
/ ?/?/?
_/?_/? _/ ?
/ ?__/? /
?/
>が出力されるようなスクリプトを書け.
> --nobsun
源馬のSchemeでの回答はこちら。~
http://www.shiro.dreamhost.com/scheme/wiliki/wiliki.cgi?g...
*解答 in Haskell [#nd70223e]
下村です。
無駄に長い上に汚いですけど…。
output str =
let result = graph str
output' :: [String] -> IO ()
output' mat =
if any null mat then return ()
else do if any (' '/=) h the...
els...
output' t
where
h = map head mat
t = map tail mat
in
output' result
graph :: String -> [String]
graph str =
let
height = (length str) * 2
graph' [] _ = []
graph' (x:xs) pos =
case x of
'R' -> oneline (pos) '/' height : graph' x...
'F' -> oneline (pos+1) '??' height : graph...
'C' -> oneline pos '_' height : graph' xs ...
in
graph' str (length str)
-- n番目の文字がcであるような、長さlの文字列を生成する
oneline :: Int -> Char -> Int -> String
oneline _ _ 0 = ""
oneline n c l = (if n==0 then c else ' ') : oneline (n-1...
で、結果は…
Main> output "RCRFCRFFCCRFFRRCRRCCFRFRFF"
__
/ ?/?/?
_/?_/? _/ ?
/ ?__/? /
?/
Main>
*解答 in OCaml [#yc2d1595]
また下村です。OCamlで書き換えたので書いときます。Haskell...
(* <<COMPILE>> "ocamlopt -I +extlib extlib.cmxa lineGrap...
open ExtString.String;;
open List;;
exception NotRFC;;
let any = List.fold_left (or) false;;
let graph charlist =
let height = (length charlist) * 2 in
let rec transpose mat = if any (map (fun x -> x=[]) mat)
then []
else map hd mat :: transpose (map tl mat) in
let rec oneline n c l = if l=0
then []
else (if n=0 then c else ' ') :: oneline (n-1) c (l-...
let rec graph' charlist pos =
match charlist with
[] -> []
| c::cs -> match c with
'R' -> oneline (pos) '/' height :: graph' cs...
| 'F' -> oneline (pos+1) '??' height :: graph' cs (po...
| 'C' -> oneline pos '_' height :: graph' cs pos
| otherwise -> raise NotRFC
in
transpose (graph' charlist (length charlist));;
let output =
try
let result =
map (fun l -> if any (map (fun x -> x<>' ') l)
then implode l ^ "?n" else "")
(graph (explode Sys.argv.(1))) in
let rec output' list = match list with
[] -> ()
| (""::ls) -> output' ls
| (l::ls) -> print_string l;
output' ls in
output' result
with
NotRFC ->
print_string ("Usage : " ^ Sys.argv.(0) ^ " [RFC]*?n")
| Invalid_argument _ ->
print_string ("Usage : " ^ Sys.argv.(0) ^ " [RFC]*?n");;
実行結果は…
mac{sho}% ./a.out RCRFCRFFCCRFFRRCRRCCFRFRFF ...
__
/ ?/?/?
_/?_/? _/ ?
/ ?__/? /
?/
mac{sho}% ...
- Ocamlがわかってないので、おぼろげにしかわからないです。...
終了行:
[[ネタ記録庫/AAGraph]]
#contents
* [[AA折れ線グラフ:http://oss.timedia.co.jp/index.fcgi/ka...
>AAで折れ線グラフを書くというお題.
>入力は'R','F','C'の3種類も文字からなる長さ1以上の文字列
>'R'は上昇を表し,折れ線グラフの要素としては '/' (スラッ...
>'F'は下降を表し,折れ線グラフの要素としては '?' (バック...
>'C'は変化なしを表し,折れ線グラフの要素としては'_'(アン...
>たとえば,
$ ./plot RCRFCRFFCCRFFRRCRRCCFRFRFF
>とすると
__
/ ?/?/?
_/?_/? _/ ?
/ ?__/? /
?/
>が出力されるようなスクリプトを書け.
> --nobsun
源馬のSchemeでの回答はこちら。~
http://www.shiro.dreamhost.com/scheme/wiliki/wiliki.cgi?g...
*解答 in Haskell [#nd70223e]
下村です。
無駄に長い上に汚いですけど…。
output str =
let result = graph str
output' :: [String] -> IO ()
output' mat =
if any null mat then return ()
else do if any (' '/=) h the...
els...
output' t
where
h = map head mat
t = map tail mat
in
output' result
graph :: String -> [String]
graph str =
let
height = (length str) * 2
graph' [] _ = []
graph' (x:xs) pos =
case x of
'R' -> oneline (pos) '/' height : graph' x...
'F' -> oneline (pos+1) '??' height : graph...
'C' -> oneline pos '_' height : graph' xs ...
in
graph' str (length str)
-- n番目の文字がcであるような、長さlの文字列を生成する
oneline :: Int -> Char -> Int -> String
oneline _ _ 0 = ""
oneline n c l = (if n==0 then c else ' ') : oneline (n-1...
で、結果は…
Main> output "RCRFCRFFCCRFFRRCRRCCFRFRFF"
__
/ ?/?/?
_/?_/? _/ ?
/ ?__/? /
?/
Main>
*解答 in OCaml [#yc2d1595]
また下村です。OCamlで書き換えたので書いときます。Haskell...
(* <<COMPILE>> "ocamlopt -I +extlib extlib.cmxa lineGrap...
open ExtString.String;;
open List;;
exception NotRFC;;
let any = List.fold_left (or) false;;
let graph charlist =
let height = (length charlist) * 2 in
let rec transpose mat = if any (map (fun x -> x=[]) mat)
then []
else map hd mat :: transpose (map tl mat) in
let rec oneline n c l = if l=0
then []
else (if n=0 then c else ' ') :: oneline (n-1) c (l-...
let rec graph' charlist pos =
match charlist with
[] -> []
| c::cs -> match c with
'R' -> oneline (pos) '/' height :: graph' cs...
| 'F' -> oneline (pos+1) '??' height :: graph' cs (po...
| 'C' -> oneline pos '_' height :: graph' cs pos
| otherwise -> raise NotRFC
in
transpose (graph' charlist (length charlist));;
let output =
try
let result =
map (fun l -> if any (map (fun x -> x<>' ') l)
then implode l ^ "?n" else "")
(graph (explode Sys.argv.(1))) in
let rec output' list = match list with
[] -> ()
| (""::ls) -> output' ls
| (l::ls) -> print_string l;
output' ls in
output' result
with
NotRFC ->
print_string ("Usage : " ^ Sys.argv.(0) ^ " [RFC]*?n")
| Invalid_argument _ ->
print_string ("Usage : " ^ Sys.argv.(0) ^ " [RFC]*?n");;
実行結果は…
mac{sho}% ./a.out RCRFCRFFCCRFFRRCRRCCFRFRFF ...
__
/ ?/?/?
_/?_/? _/ ?
/ ?__/? /
?/
mac{sho}% ...
- Ocamlがわかってないので、おぼろげにしかわからないです。...
ページ名: