單行小程式
Sign in to savetextual input to the command-line of an operating system shell that performs some function in just one line of input
Article · 中文
單行小程式(One-liner program)是不多於一行的電腦程序或表達式。它常被用来展示程式語言在不同範疇的表達能力。實際上,單行小程式常被直接地從命令線輸入和執行。Perl是一種優秀的用来創造單行小程式的語言。許多單行小程式是實用的,例如以下perl單行小程式將反轉文件中的所有位元組: perl -0777e 'print scalar reverse <>' filename 單行小程式经常用來展示編程的能力。經常有看誰能創造最好的單行小程式的比賽。 99瓶啤酒電腦程式單行小程式的例子: perl -e '$b="of beer";$w="on the wall";print "$_ ",b($_)," $b $w $_ ",b($_)," $b take one down, pass it around, ",$_-1," ",b($_-1)," $b $w
" foreach (reverse 1..99);sub b{$x="bottle";$_[0]==1?$x:$x."s";}' 一個剪刀石頭布遊戲單行小程式: perl -e '$c=(($r,$s,$p)=qw|rock scissors paper|)[(int rand 3)];$|=0;until($y=~/^($r|$s|$p)$/){print "$r, $s or $p? ";$y=<>;chop $y};%r=($s,{$p,1,$r,0},$p,{$r,1,$s,0},$r,{$s,1,$p,0});print "You: $y Com: $c You ".((exists $r{$y}{$c}?($r{$y}{$c}?"Win":"Lose"):"Draw")." ")' Perl不是唯一一個能写出單行小程式的语言。以下是顯示輸入的内容的ASCII碼的C程式: void main(void) {int c; while(c = getchar) printf("%d %02X ", c, c);}
Abstract from DBpedia / Wikipedia · CC BY-SA