LoginSignup
1
1

More than 3 years have passed since last update.

いろんな言語で print を print する

Last updated at Posted at 2019-12-11

はじめに

文字列を出力する命令(コマンド、関数、etc)の対象や引数に、その命令自体を指定した際の結果をいろんな言語で試してみます。

自分が普段使っているLuaの後は
ブラウザでプログラミング・実行ができる「オンライン実行環境」| paiza.IO
にて実施します。(どうしてここにLuaがないんですか? 電話猫略)

おおむね以下の方法で実施します。

  • 文字列出力のサンプルがあらかじめ入っている言語については"XXXXXXXX"を文字出力を表すであろう語句に差し替えます
  • もしなければ'Hello World' 等で検索したコードを参考に記述します

おそらく結果は環境に左右されるものもかなり含んでいると思います。

Lua

pripri.lua
print(print)

出力


function: 0000000061d18e60

公式が提供している lua.exe (lua-5.3.5_Win64_bin)にて実施
(感想)
Luaではprintという変数に「出力を行なう関数実体への参照(関数型の値)が割り当てられている」ような感じ。(文字列型でない場合は自動で文字列化されている)


ここから 「ブラウザでプログラミング・実行ができる「オンライン実行環境」| paiza.IO」の結果です。

Bash

Main.sh
# Your code here!

echo echo

出力

echo

(感想)
シェルでは文字列は(クォートなどでくくったりしなくても)文字列として解釈されるのでこうなるってことかな(多分)

C

Main.c
#include <stdio.h>
int main(void){
    // Your code here!
    printf(printf);
}

出力

�%

(感想)
分からん、全然分からん!(でもエラーはなし)
関数ポインタの値を単にbyte列として出力?
ちなみにprintf("%s",printf);としても同様

C#

Main.cs
public class Hello{
    public static void Main(){
        // Your code here!
        System.Console.WriteLine(System.Console.WriteLine);
    }
}

コンパイルエラー

Compilation failed: 2 error(s), 0 warnings
Main.cs(5,24): error CS1502: The best overloaded method match for `System.Console.WriteLine(bool)' has some invalid arguments
/usr/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
Main.cs(5,49): error CS1503: Argument `#1' cannot convert `method group' expression to type `bool'

C++

Main.cpp
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    cout << cout << endl;
}

コンパイルエラー

Main.cpp:5:10: error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'std::ostream')
    cout << cout << endl;
    ~~~~ ^  ~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:245:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'const void *' for 1st argument; take the address of the argument with &
      operator<<(const void* __p)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/system_error:217:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'const std::error_code' for 2nd argument
    operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:108:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'std::basic_ostream<char, std::char_traits<char> >::__ostream_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ostream_type &)' (aka 'basic_ostream<char, std::char_traits<char> > &(*)(basic_ostream<char, std::char_traits<char> > &)') for 1st argument
      operator<<(__ostream_type& (*__pf)(__ostream_type&))
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:117:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'std::basic_ostream<char, std::char_traits<char> >::__ios_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ios_type &)' (aka 'basic_ios<char, std::char_traits<char> > &(*)(basic_ios<char, std::char_traits<char> > &)') for 1st argument
      operator<<(__ios_type& (*__pf)(__ios_type&))
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:127:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'std::ios_base &(*)(std::ios_base &)' for 1st argument
      operator<<(ios_base& (*__pf) (ios_base&))
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:166:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'long' for 1st argument
      operator<<(long __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:170:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'unsigned long' for 1st argument
      operator<<(unsigned long __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:174:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'bool' for 1st argument
      operator<<(bool __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:178:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'short' for 1st argument
      operator<<(short __n);
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:181:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'unsigned short' for 1st argument
      operator<<(unsigned short __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:189:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'int' for 1st argument
      operator<<(int __n);
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:192:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'unsigned int' for 1st argument
      operator<<(unsigned int __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:201:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'long long' for 1st argument
      operator<<(long long __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:205:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'unsigned long long' for 1st argument
      operator<<(unsigned long long __n)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:220:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'double' for 1st argument
      operator<<(double __f)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:224:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'float' for 1st argument
      operator<<(float __f)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:232:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'long double' for 1st argument
      operator<<(long double __f)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:270:7: note: candidate function not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'std::basic_ostream<char, std::char_traits<char> >::__streambuf_type *' (aka 'basic_streambuf<char, std::char_traits<char> > *') for 1st argument
      operator<<(__streambuf_type* __sb);
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:502:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'char' for 2nd argument
    operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:508:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'char' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, char __c)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:514:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'signed char' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:519:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'unsigned char' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:556:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'const char *' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:569:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'const signed char *' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:574:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'const unsigned char *' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/ostream.tcc:321:5: note: candidate function template not viable: no known conversion from 'std::ostream' (aka 'basic_ostream<char>') to 'const char *' for 2nd argument
    operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:497:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::basic_ostream<char>')
    operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/string_view:559:5: note: candidate template ignored: could not match 'basic_string_view' against 'basic_ostream'
    operator<<(basic_ostream<_CharT, _Traits>& __os,
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/basic_string.h:6284:5: note: candidate template ignored: could not match 'basic_string' against 'basic_ostream'
    operator<<(basic_ostream<_CharT, _Traits>& __os,
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:539:5: note: candidate template ignored: could not match 'const _CharT *' against 'std::ostream' (aka 'basic_ostream<char>')
    operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/ostream:682:5: note: candidate template ignored: requirement '__and_<__not_<is_lvalue_reference<basic_ostream<char> &> >, __is_convertible_to_basic_ostream<basic_ostream<char> &>, __is_insertable<__rvalue_ostream_type<basic_ostream<char> &>, const basic_ostream<char> &> >::value' was not satisfied [with _Ostream = std::basic_ostream<char> &, _Tp = std::basic_ostream<char>]
    operator<<(_Ostream&& __os, const _Tp& __x)
    ^
1 error generated.

(感想)
怒りすぎやろ‥‥

Main.cpp
#include <stdio.h>

int main()
{
    puts(puts);
}

という書き方だと↓のようなエラーメッセージ。

Main.cpp:5:5: error: no matching function for call to 'puts'
    puts(puts);
    ^~~~
/usr/include/stdio.h:632:12: note: candidate function not viable: no known conversion from 'int (const char *)' to 'const char *' for 1st argument
extern int puts (const char *__s);
           ^
1 error generated.

Clojure

Main.clj
; Your code here!
(println println)
#object[clojure.core$println 0x61af1510 clojure.core$println@61af1510]

(感想)
Luaと似た感じ?

Cobol

Main.cob
IDENTIFICATION DIVISION.
program-id. Hello.
procedure division.
# Your code here!

display display.

コンパイルエラー

Main.cob:4: Error: syntax error, unexpected $undefined

CoffeeScript

Main.coffee

# Your code here!

console.log(console.log)

出力

[Function: bound consoleCall]

D

Main.d
// Your code here!

import std.stdio;
void main(){
  writeln(writeln);
}

コンパイルエラー

Main.d(5): Error: template `std.stdio.writeln` cannot deduce function from argument types `!()(void)`, candidates are:
/usr/local/ldc2-1.18.0-linux-x86_64/bin/../import/std/stdio.d(3926):        `writeln(T...)(T args)`

Elixir

Main.exs

# Your code here!

IO.puts IO.puts

実行時エラー

** (UndefinedFunctionError) function IO.puts/0 is undefined or private. Did you mean one of:

      * puts/1
      * puts/2

    (elixir) IO.puts()
    Main.exs:3: (file)
    (elixir) lib/code.ex:813: Code.require_file/2

Erlang

main.erl
-module(main).
-export([main/1]).
%% Your code here!

main([_]) ->
    io:format(io:format),
init:stop() .

コンパイルエラー

main.erl:6: illegal expression

F#

Main.fs
// Your code here!
printfn printfn

コンパイルエラー

F# Compiler for F# 4.0 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License

/workspace/Main.fs(2,9): error FS0001: The type ''b -> 'c' is not compatible with the type 'Printf.TextWriterFormat<'a>'

Go

Main.go
package main
import "fmt"
func main(){
    // Your code here!

    fmt.Println(fmt.Println)
}

出力

0x486800

Haskell

Main.hs
-- Your code here!

main = putStrLn putStrLn

コンパイルエラー

[1 of 1] Compiling Main             ( Main.hs, Main.o )

Main.hs:3:17: error:
    • Couldn't match type ‘String -> IO ()’ with ‘[Char]’
      Expected type: String
        Actual type: String -> IO ()
    • Probable cause: ‘putStrLn’ is applied to too few arguments
      In the first argument of ‘putStrLn’, namely ‘putStrLn’
      In the expression: putStrLn putStrLn
      In an equation for ‘main’: main = putStrLn putStrLn

Java

Main.java
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!

        System.out.println(System.out.println);
    }
}

コンパイルエラー

Main.java:7: error: cannot find symbol
        System.out.println(System.out.println);
                                     ^
  symbol:   variable println
  location: variable out of type PrintStream
1 error

JavaScript (Node.js)

Main.js

process.stdin.resume();
process.stdin.setEncoding('utf8');
// Your code here!

console.log(console.log);

出力

[Function: bound consoleCall]

Kotlin

Main.kt
fun main(args: Array<String>) {
    // Your code here!

    println(println)
}

コンパイルエラー

OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Main.kt:4:13: error: function invocation 'println()' expected
    println(println)

MySQL

Main.sql
create table Test(id integer, title varchar(100));
insert into Test(id, title) values(1, values);
select * from Test;
-- Your code here!

実行時エラー

ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

Objective-C

Main.m
#import <Foundation/Foundation.h>
int main(void){
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    // Your code here!

    NSString *s = @writeData;
    [[NSFileHandle fileHandleWithStandardOutput] writeData: [s dataUsingEncoding: NSUTF8StringEncoding]];
    [pool release];
    return 0;
}

コンパイルエラー

Main.m:6:19: error: unexpected '@' in program
    NSString *s = @writeData;
                  ^
1 error generated.
Main.m
#import <Foundation/Foundation.h>
int main(void){
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    // Your code here!

    NSString *s = @"XXXXXXXX";
    [[NSFileHandle fileHandleWithStandardOutput] writeData: [writeData dataUsingEncoding: NSUTF8StringEncoding]];
    [pool release];
    return 0;
}

だとエラーメッセージは

Main.m:7:62: error: use of undeclared identifier 'writeData'
    [[NSFileHandle fileHandleWithStandardOutput] writeData: [writeData dataUsingEncoding: NSUTF8StringEncoding]];
                                                             ^
1 error generated.

Perl

Main.pl
# Your code here!

print print

出力

1

(感想)
え、そうなんだ?

PHP

Main.php
<?php
// Your code here!
echo echo;
?>

実行時エラー

PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO) in /workspace/Main.php on line 3

Python2

Main.py
# coding: utf-8
# Your code here!

print print

実行時エラー

  File "Main.py", line 4
    print print
              ^
SyntaxError: invalid syntax

print(print)でも同様のエラー

Python3

Main.py
# coding: utf-8
# Your code here!

print(print)

出力

<built-in function print>

(感想)
Python2とは別物なんですね。

R

Main.R
# Your code here!

cat(cat)

実行時エラー

Error in cat(cat) : 
  argument 1 (type 'closure') cannot be handled by 'cat'
Execution halted

Ruby

Main.rb
# Your code here!

print print

出力なし

Main.rb
# Your code here!

puts puts

出力




改行2つ(多分)

Main.rb
# Your code here!

p p

出力

nil

Rust

Main.rs
// Your code here!

fn main(){println!(println);}

コンパイルエラー

error: format argument must be a string literal
 --> Main.rs:3:20
  |
3 | fn main(){println!(println);}
  |                    ^^^^^^^
help: you might be missing a string literal to format with
  |
3 | fn main(){println!("{}", println);}
  |                    ^^^^^

error: aborting due to previous error

Scala

Main.scala
object Main extends App{
    // Your code here!

    println(println)
}

出力



()

(感想)
ほー

Scheme

Main.scm
; Your code here!

(define (main args) (print print) 0)

出力

#<closure (print . args)>

Swift

main.swift
// Your code here!

print(print)

出力

(Function)

VB

Main.vb

public class compiler
  shared function Main as integer
' Your code here!

    Console.WriteLine (Console.WriteLine)
    return 0
  End function
end class

コンパイルエラー

Visual Basic.Net Compiler version 0.0.0.5943 (Mono 4.0.1 - tarball)
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.

/workspace/Main.vb (5,32) : error VBNC30491: Expression does not produce a value.

There were 1 errors and 0 warnings.

Compilation took 00:00:00.4335610

まとめ

全体的にはコンパイル系はコンパイルエラーを出すものが多い印象ですね。
個人的にはCやPerl、Scala辺りが印象深いです。(あとC++)
自分には知識も技術もないのでまともな感想になりませんでしたが、皆さんはどうでしたでしょうか?

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1