ときどきの雑記帖 RE* (新南口)
日々憶測
バウアー
今日のハマスタはハマスタ史上最高の入りだったとか。
- DeNA・バウアーが来日初登板・初勝利!ヒーローインタビューでは日本語で「ありがとう!」、「横浜しか勝たん!」 | ショウアップナイター
- DeNAバウアー1軍初登板、プロ野球ファン熱視線 「ホームランでご挨拶は最高」「すげえ投げづらそう」:中日スポーツ・東京中日スポーツ
L字
横から見るとL字になっていて(小さな)子供が乗っかれるようなキャリーケースを見かけた。 あんなのあったのか。
ギャ vs ガ
姓はフランス語の発音に近い「ゴーガン」とも表記され、近年の美術展覧会などではこちらを採用する例も多い。
へー。
ring
redditの How can (e & f) ^ (~e & g) be written as (g ^ (e & (f ^ g)))? : r/programming で取り上げられていた話題で
hash-library/sha256.cpp at master · stbrumme/hash-library
// mix functions for processBlock()
inline uint32_t f1(uint32_t e, uint32_t f, uint32_t g)
{
uint32_t term1 = rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25);
uint32_t term2 = (e & f) ^ (~e & g); //(g ^ (e & (f ^ g)))
return term1 + term2;
}
(redditのスレッドのタイトルにもあるけど)
(e & f) ^ (~e & g)
は
(g ^ (e & (f ^ g)))
に置き換えられると。
ぱっと見ではあまりありがたみを感じないけど
論理演算が一回減るのですねこれ。
やっつけスクリプトを組んで狭い範囲(e, f, gがそれぞれ10ビット程度)で 試したところでは確かに同じ結果になった。 が、本当にどんな場合でも成り立つのだろうかと 疑問に思ったのだけど、 スレの発言の一つによれば
These are bitwise operations so you can consider them bit by bit. & and ^ form the Z/Z2 ring
(& is *, ^ is +)
.~e is just (1 + e)
ef+(1+e)g = ef+g+eg = g+ef+eg = g+e*(f+g)
なのだと。 へー。
新刊近刊
デジタル生存競争
内容はさておき(マテ)、イラストの人にちょっと驚いた。
When will it be done?
こっちは新刊ではないのだけど redditで見かけたのでメモ。
- Rules of Thumb for Software Development Estimations
- Rules of Thumb for Software Development Estimations : r/programming
I recommend the book “When will it be done?” by Dan Vacanti. The basic idea is to combine these three things: Use software/maths to make forecasts, based on data you’re already tracking anyway. Do it all the time, not just once at the start. All forecasts come in the form “I am x% sure I can do it before date y”. In other words, a time range and a percentage. As a rule of thumb, when the certainty percentage goes up, the date gets further away. Split the work into reasonably sized chunks, and try to keep an up-to-date list of todo chunks while working. That way the forecasting software has something to work with.
翻訳本はなさげ?
Egison
メモその2。 意外なところで、と言っては失礼かもしれないけど HNでその名前を見るとは思わなかった。
HNのスレッドを見ると過去にも話題になったことがあるようで (気がつかなかったか忘れていたか)。
Solving balanced parentheses problem using Dart’s type system
メモその3。
みんな大好きカッコのバランス問題を
- Solving balanced parentheses problem using Dart’s type system : r/programming
- Balanced parentheses with Dart 3
Two types of C programmers
メモその4。
Here is a provocative thesis:
There are two types of C programmers: people who chose C because they liked various of its properties, or people who used C because it was their best or only option at the time.
switch
結構前の話題なようなのだけど、紹介のメールが(Quoraから)送られてきたので読んだ。
switch文は、なぜ自分でbreakを補わなければならない仕様になっているのでしょうか?breakを補って使うことがほとんどであるため、理解できません。 - コード - Quora
いくつか回答がついているのだけど、 どれも質問に対する回答になっていないような印象がある。 たとえば、現行のCのswitchの仕様で便利に使える、有効であるという 事例を挙げることはなぜそのような仕様になったのかを 説明することにはならない(少なくとも直接には) と思うのだけど。
あるいは
気に入らなければ if 文使えば良いだけの事です。以下に switch と if で同じ処理をするコードを書いてみました。 後はどちらの書き方が好きかという問題だと思います。
こういうのもちょっと… (質問の最後の「理解できません」に対する回答ではある?)
それと
もともとswitch文が発明されたC言語において、switch文の分岐先caseラベル「case <定数値>:」は、 コロンがあることから見てもわかるように「ラベル」であり、switch文自体言語機能として「テーブルジャンプ」 として発想されたからだと思われます。つまりswitch式にあたえられた結果のラベルに「ジャンプ」するのです。
これはどうなんだろう。
(その後放置してしまっているけど) XCとgccの比較記事でもあったと思うのだけど switchでテーブルジャンプを使うように コンパイルするのってわりと面倒な話で、 関係が逆 (ある限定的な局面でswitch文をテーブルジャンプを使ったコードにコンパイルできることもある) じゃないかなあ。
んで、 c - Why was the switch statement designed to need a break? - Stack Overflow
To allow things like:
switch(foo) { case 1: /* stuff for case 1 only */ if (0) { case 2: /* stuff for case 2 only */ } /* stuff for cases 1 and 2 */ case 3: /* stuff for cases 1, 2, and 3 */ }
などを見て、そういやあこの辺りの構文定義ってどうなっていたのだっけ? という疑問がわき。
知って損はない、各言語のBNF定義 - slow living in the sky
こちらは前項のような可視化やリンクなどの補助がないので、より正確に読み取る必要があります。 C言語のif文(switch文も含む)は次のように定義されています。
<selection-statement> ::= if ( <expression> ) <statement> | if ( <expression> ) <statement> else <statement> | switch ( <expression> ) <statement>
さらにstatementは次のように。
<statement> ::= <labeled-statement>
| <expression-statement>
| <compound-statement>
| <selection-statement>
| <iteration-statement>
| <jump-statement>
<labeled-statement> ::= <identifier> : <statement>
| case <constant-expression> : <statement>
| default : <statement>
なるほどこうなってたのか。 今まで意識していなかった😓
参考までにpcc GitHub - IanHarvey/pcc: Portable C Compiler sources の構文定義から (pccにした理由は古くからあるものでya㏄を使っている(=構文定義が見やすい)から)。
pcc/cgram.y at master · IanHarvey/pcc
statement: e ';' { ecomp(eve($1)); symclear(blevel); }
| compoundstmt
| ifprefix statement { plabel($1); reached = 1; }
| ifelprefix statement {
if ($1 != NOLAB) {
plabel( $1);
reached = 1;
}
}
...
| switchpart statement
{ if( reached ) branch( brklab );
plabel( $1 );
swend();
plabel( brklab);
if( (flostat&FBRK) || !(flostat&FDEF) ) reached = 1;
resetbc(FCONT);
}
| C_BREAK ';' {
if (brklab == NOLAB)
uerror("illegal break");
else if (reached)
branch(brklab);
flostat |= FBRK;
reached = 0;
}
...
| label statement
;
pcc/cgram.y at master · IanHarvey/pcc
switchpart: C_SWITCH '(' e ')' {
P1ND *p;
int num;
TWORD t;
savebc();
brklab = getlab();
$3 = eve($3);
if (!ISINTEGER($3->n_type)) {
uerror("switch expression must have integer "
"type");
t = INT;
} else {
$3 = intprom($3);
t = $3->n_type;
}
p = tempnode(0, t, 0, 0);
num = regno(p);
ecomp(buildtree(ASSIGN, p, $3));
branch( $$ = getlab());
swstart(num, t);
reached = 0;
}
;
pcc/cgram.y at master · IanHarvey/pcc · GitHub
label: C_NAME ':' attr_var { deflabel($1, $3); reached = 1; }
| C_TYPENAME ':' attr_var { deflabel($1, $3); reached = 1; }
| C_CASE e ':' { addcase(eve($2)); reached = 1; }
/* COMPAT_GCC */| C_CASE e C_ELLIPSIS e ':' {
#ifdef GCC_COMPAT
gcccase(eve($2), eve($4)); reached = 1;
#endif
}
| C_DEFAULT ':' { reached = 1; adddef(); flostat |= FDEF; }
ところで検索していて Cコンパイラ設計(yacc・lexの応用) というのを見つけ、 この本持っていたんだけど借りパクされたんだよなあ と遠い目に。
ところで(その2)
G・フリードマンの「Cコンパイラ設計」は、yacc/lexの使い方を丁寧に説明した名著であるが、英語の原本も日本語本も絶版で、 amazonのオークションでも買えないため日本語訳本を借り、OCRから復刻することにした。
いいのか、それ。
閑話休題。
そういやCならリッチーの書いた Chistory があるじゃん。 ということで 改めて見てみたのだけど
Not every difference between the BCPL language documented in Richards’s book [Richards 79] and B was deliberate; we started from an earlier version of BCPL [Richards 67]. For example, the endcase that escapes from a BCPL switchon statement was not present in the language when we learned it in the 1960s, and so the overloading of the break keyword to escape from the B and C switch statement owes to divergent evolution rather than conscious change.
なんて記述があった。 そこでBやらBCPLはどうだったのかとみてみると
B
User’s reference to B にこんなコードがあった。
printf(fmt, x1 ,x2,x3,x4,x5,x6,x7,x8,x9) {
extrn printn, char, putchar;
auto adx, x, c, i, jj;
i= O; /* fmt index */
adx = &xl; /* argument pointer */
loop :
while((c=char(tmt,i++) ) != '%') {
if(c == '*e')
return;
putchar(c);
}
x = *adx++;
switch C = char(fmt,i++) {
case 'd': /* decimal */
case 'o': /* octal */
if(x < O) {
x = -x ;
putchar('-');
}
printn(x, c=='o'?8:1O);
goto loop;
case 'c' : /* char */
putchar(x);
goto loop;
case 's': /* string */
j = 0;
while((c=char(x,j++)) != '*e')
putchar(c);
goto loop;
}
putchar('%');
i--;
adx--;
goto loop;
}
C Historyにあるように、(switch文で使われる)break
はないけれども
switch~caseはあって、さらにgotoの使い方を見てもわかるように
Cのswitch~case同様 fall throgthするよね。これ。
BCPL
じゃあ、ともう一段遡ってBCPLはというと
https://www.cl.cam.ac.uk/~mr10/bcplman.pdf
2.3.5 SWITCHON command
A SWITCHON command has the following form:SWITCHON E INTO { C1 ;...; Cn }
Labels of the form DEFAULT: or CASE K: are permitted in the command se- quence. E is evaluated and control is passed to the matching case label if it exists, otherwise a jump is made to the default label but, if that is not given, control passes to the point just after the switchon command.
2.3.8 Flow of Control
ENDCASE causes execution to jump to the point just after the end of the smallest enclosing SWITCHON command which must be within the current function or routine.
http://bitsavers.trailing-edge.com/pdf/mit/tx-2/TX-2_BCPL_Reference_Manual_May69.pdf
Semantics: The expression is first evaluated and if a case exists which has a constant with the same arithmetic value then execution is resumed at that label; otherwise, if there is a default label then execution is continued from there, and if there is not, execution is resumed just after the end of the switch on command.
The switch is implemented as direct switch, a sequential search or a tree search depending on the number and range of the case constants.
switchon
と微妙に異なる予約語ではあるけれども同様の構文はあると。
Bのサンプルプログラムのようなはっきりわかる例は見つからなかったけど、
(後付けかどうかはともかく)endcase
の存在を考えると
BCPLでもやはりfall throughしたんじゃなかろうか。
CPL
さらに前のCPLはわからん。
おまけ
- Portable C Compilerから始めようと思った。(いまここ編):docker(118) - Qiita
- 7.6. Conditionals
- Multiway branch - Wikipedia
- Switch statement - Wikipedia
In his 1952 text Introduction to Metamathematics, Stephen Kleene formally proved that the CASE function (the IF-THEN-ELSE function being its simplest form) is a primitive recursive function,
Languages derived from C language, and more generally those influenced by Fortran’s computed GOTO,
FORTRAN Compiler on IBM 704
COBOL
HNで
- Learning COBOL: A Journey for the Modern Programmer (2021) | Hacker News
- Learning COBOL: A Journey for the Modern Programmer - HedgeDoc
というのを見かけ、COBOLの最初期の実装も見てみたいものだけど あるんすかねえ。あってもUNIVAC上のものだろうから これまた704のように追いかけるのが大変そうではある。
COBOLの直接の祖先ということでFLOW-MATICも以下略。
LAMBDA
という与太はさておき。 このところちまちま追い続けている level analysisのつづき。
例の3LBARがらみでストアしてるアドレスを追いかけてみた、
LAMBDA,A STO C(ALPHA+A-1) IN LAMBDA+3L 4F13898
LAMBDA,A STO C(ALPHA+A-2) IN LAMBDA+3L 4F13949
LAMBDA,A STO C(ALPHA+A-3) IN LAMBDA+3L 4F13906
LAMBDA+1,A 4F13824
LAMBDA+2,A STO S(N) IN LAMBDA+3L+2 4F13910
LAMBDA+2,A STO S(N) IN LAMBDA+3L+2 4F13953
LAMBDA+3,A 4F13817
LAMBDA+3,A STO -0 IN LAMBDA+3(L+1) 4F13895
LAMBDA+3,A STO -N IN LAMBDA+3(L+1) 4F13954
LAMBDA+3,A STO -N IN LAMBOA+3(L+1) 4F13911
LAMBDA+4,A STO * IN LAMBDA+3(L+1)+1 4F13920
LAMBDA+5,A 4F13803
LAMBDA+5,A STO S(N+1) IN LAMBDA+3(L+1)+2 4F13914
LAMBDA+5,A STO S(N+1) IN LAMBDA+3(L+1)+2 4F13883
LAMBDA+6,A 4F13816
LAMBDA+6,A 4F13946
LAMBDA+6,A STO -(N+1) IN LAMBDA+3(L+2) 4F13917
LAMBDA+6,A STO -N IN LAMBDA+3(L+2) 4F13892
LAMBDA+7,A 4F13821
LAMBDA+7,A STO SPOP IN LAMBDA+3(L+2)+1 4F13926
LAMBDA+8,A 4F13802
LAMBDA+8,A STO 5(N+2) IN LAMBDA+3(L+2)+2 4F13934
LAMBDA+8,A STO S(N+1) IN LAMBDA+3(L+2)+2 4F13888
LAMBDA+8,A STOS(N+2) IN LAMBDA+3(L+2)+2 4F13923
LAMBDA+9,A 4F13815
LAMBDA+9,A STO -(N+1) IN LAMBDA+3(L+3) 4F13943
LAMBDA+9,A STO -(N+2) IN LAMBDA+3(L+3) 4F13924
LAMBDA+9,A STO -0 IN LAMBDA +3(L+3) 4F13873
LAMBDA+9,A STO -0 IN LAMBDA+3(L+3) 4F13903
LAMBDA+10,A 4F13820
LAMBDA+10,A STO SPOP IN LAMBDA+3(L+3)+1 4F13930
LAMBDA+11,A 4F13601
LAMBDA+11,A STO S(N+2) IN LAMBDA+3(L+3)+2 4F13939
LAMBDA+11,A STO S(N+3) IN LAMBDA +3(L+3)+2 4F13852
LAMBDA+12,A STO -(N+2) IN LAMBDA+3 (L+4) 4F13839
LAMBDA+13,A 4F13819
LAMBDA+14,A STO (N+3) IN LAMBDA+3 (L+4)+2 4F13834
LAMBDA,1 4F13574
LAMBDA+1,1 4F13576
LAMBDA+2,1 4F13579
LAMBDA+0からLAMBDA+14までの15個で 3の倍数だよなあとか コメントをみても×3があるっぽい。
その一方でここで使っているインデックスレジスターA(実体は1番レジスター) に対する操作は15もないので どういうこと?🤔
Waterloo
"Waterloo Style" - 複雑なソフトウェアも、データ処理の視点から考えればそれほど複雑ではない。多くの場合、アルゴリズムに気をつかう必要があるのは一部である。そのため、データフローを追うことが複雑なソフトウェアを理解する近道である。https://t.co/atyOE42ocT
— 新山祐介 (Yusuke Shinyama) (@mootastic) April 30, 2023
CSでは有名なところすね> University of Waterloo
もし ChatGPTが StackOverflowから学習したら…
— 新山祐介 (Yusuke Shinyama) (@mootastic) April 30, 2023
「その処理にPythonなんか使うべきじゃないね」
「それは馬鹿な質問ですね」
「この質問はすでに回答されました」
「そんなことをするのは馬鹿です、やめましょう」https://t.co/5e6bJoJEwZ
なんかひっかかるものがあったので(ry
If ChatGPT learned from Stack Overflow : r/ProgrammerHumor
■I want to sort a list alphabetically while also retaining the number ordering (1 > 2 > 10). How would I do this in Python?
●You should’nt use Python for this.
■Okay what would you suggest I use instead?
●That’s a stupid question.
■I managed to solve this (see the image.png for how), but I ran into another issue. I’m reading files from a folder. I did find out how to make sure the file exist like below:
from pathlib import Path file = Path("/path/to/file") if file.is_file(): #File exists
But how do I make sure the files are not corrupted?
●This question has already been asked.
■That duplicate question wasn’t really related to this problem and it wasn’t even in Python. In the end I decided to just check for the corrupted files manually.
New problem: I want to add timestamps (current time) to the output files, but I can’t get it to work consistently if some users are different timezones. Is there someway to unify the output so timezon has no effect the result?
●You should never do that. That’s just stupid.
なるほど。 ツイートには一連のやり取りの ChatGPTからの出力のみがあったのね。