確かにベクトルのような型について v1 + a*v2
といったように簡潔な式で書きたい、という要求はよくわかるものではあるが、プログラミング言語における演算子の構文の簡潔さに騙されてはいけない。その構文の裏にあるセマンティクス(意味)は実にめんどくさく、それがJavaなどの言語に演算子オーバーローディングが無い理由である。以下ではいくつかのケーススタディなどを見てゆく。
BasicやAWKなど、そもそも
#!/bin/sh | |
export CLASSPATH=/usr/local/share/java/classes.zip | |
export JAVA_HOME=/usr/local/share/java | |
exec /usr/local/lib/netscape/netscape.bin -java sun.tools.javac.Main $* |
#! /usr/local/bin/ruby27 | |
module Expr2RPN | |
class Node | |
def method_missing mname, *args | |
unless [:+, :*].include? mname then | |
super | |
end | |
args_length = args.length | |
if args_length != 1 then |
#include <sys/types.h> | |
ssize_t __sys_write(int, const void *, size_t); | |
void __sys_exit(int); | |
void hoge_start(void) { | |
char const msg[] = "hello\n"; | |
void **user_stack_ptr = __builtin_frame_address(0); |
#! /usr/local/bin/ruby25 | |
class Integer | |
alias :' >' :'>' | |
alias :' <' :'<' | |
def >(other) | |
if self.send(:' >', other) then | |
other | |
else |
#include <assert.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <sys/procctl.h> |
-- FizzBuzz | |
main :: IO () | |
main = mapM_ putStrLn $ map f [1..] | |
where | |
f x = g x | |
where | |
g :: Integer -> String | |
g | x`mod`15 == 0 = (\_ -> "FizzBuzz") | |
| x`mod` 5 == 0 = (\_ -> "Buzz") | |
| x`mod` 3 == 0 = (\_ -> "Fizz") |
{m55_changebracket|[|]}[m55_dnl] | |
[m55_define|shift|`[$%[m55_define|$1|$$%]]'][m55_dnl] | |
[m55_define|NUMERAL|`[$#[m55_define|$#|`{[NUMERAL_AUX|$%]}'][m55_define|1|{}]]'][m55_dnl] | |
[m55_define|NUMERAL_AUX|`[$#[m55_define|$#|`[NUMERAL_AUX|[shift|$%]],{[NUMERAL_AUX|[shift|$%]]}'][m55_define|2|{}]]'][m55_dnl] | |
[NUMERAL] | |
[NUMERAL|] | |
[NUMERAL||] | |
[NUMERAL|||] | |
[NUMERAL||||] |
parFlatten2 :: [[a]] -> [a] | |
parFlatten2 lst = result | |
where | |
(result, tmp) = parFlattenLoop tmp lst | |
parFlattenLoop y [] = (r, []) | |
where | |
(r, t) | |
| null y = ([], undefined) | |
| otherwise = parFlattenLoop t y | |
parFlattenLoop y ([]:xss) = parFlattenLoop y xss |
parFlatten :: [[a]] -> [a] | |
parFlatten = parFlattenLoop [] | |
where | |
parFlattenLoop [] [] = [] | |
parFlattenLoop tmp [] = parFlattenLoop [] (reverse tmp) | |
parFlattenLoop tmp ([]:xss) = parFlattenLoop tmp xss | |
parFlattenLoop tmp ((x:xs):xss) = x : parFlattenLoop (xs:tmp) xss |