40
\$\begingroup\$

Produce only the name of a programming language using only the facilities of the language itself. The name of the language can only be invoked indirectly, i.e., from non custom environment variables, built-in functions or methods or the interpreter/compiler of the language. Output should be the name of the language only.

One example would be:

$ awk --v | awk 'NR<2 { print $2}'  # GNU Awk 3.1.8
Awk

The answer I select will be the one with the most up votes. In case of a tie, the shortest golfed version (separate or the only given answer), will be the tie breaker.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ so using Java reflection API is not OK? \$\endgroup\$
    – Ming-Tang
    Feb 21, 2011 at 1:15
  • \$\begingroup\$ I didn't think of that, but that's okay too. \$\endgroup\$
    – Ty Auvil
    Feb 21, 2011 at 12:20
  • \$\begingroup\$ @George: Huh? Is that now a code-golf? Why? It wasn't before ... \$\endgroup\$
    – Joey
    Feb 21, 2011 at 19:03
  • \$\begingroup\$ @Joey: No it wasn't before, but it seemed like a code golf question, so I edited it and someone approved the edit. \$\endgroup\$ Feb 21, 2011 at 19:35
  • \$\begingroup\$ @Joey: Code Golf is the tie breaker. I didn't put that TAG on originally, but George suggested it and I agreed. \$\endgroup\$
    – Ty Auvil
    Feb 21, 2011 at 23:11

86 Answers 86

1 2
3
0
\$\begingroup\$

Objective-C - 151 Chars

Little bit of trickery by taking the class name of NSObject, and chopping off the 'NS'

id s=[NSMutableString stringWithFormat:@"%@",[NSObject class]];
[s deleteCharactersInRange:NSMakeRange(0,2)];NSLog(@"%@%c%c%c%c%c",s,105,118,101,45,67);
\$\endgroup\$
2
  • \$\begingroup\$ 105,118,101,45,67 aren't system variables - are they? \$\endgroup\$ Mar 27, 2012 at 13:16
  • \$\begingroup\$ @userunknown they are the ascii codes of "ive-c". disqualify or not? \$\endgroup\$
    – tbodt
    Feb 22, 2014 at 23:13
0
\$\begingroup\$

Q (6 chars)

10h$81
\$\endgroup\$
0
\$\begingroup\$

bc

ipv4=$(ipmaddr show lo ipv4 | tail -n1 | awk '{print $2}') && echo "ibase=10;obase=16;"$(echo "ibase=6;$((2**${#ipv4}))" | bc) | bc 

Let's deconstruct it:

ipmaddr show lo ipv4
1:  lo
    inet  224.0.0.1

result is unexpected, I expected 127.0.0.1, but 224.0.0.1 is sufficient.

  • tail -n1 takes the last line.
  • awk '{print $2}' only the ip, so ipv4=224.0.0.1 now.
  • ${#ipv4} is the length of this String which is 9 (our magic number :) )
  • $((2**${#ipv4})) 2^9 is 512 as we all know.
  • echo "ibase=6;$((2**${#ipv4})) | bc interprets 512 as a number to the base 6, which is 188 in decimal
  • echo "ibase=10;obase=16;"188 | bc converts it to hexadecimal, which is - surprise:

bc

Note: A shorter solution should be

 echo "ibase=6;obase=16;512" | bc

for the right part of the &&, but somehow it doesn't work as expected.

\$\endgroup\$
0
\$\begingroup\$

C, 39 chars

main(){putchar(2[(char*)strerror(0)]);}
\$\endgroup\$
0
\$\begingroup\$

Element, 30 characters

2'116 110[2:1-+'101"]7'69[,#`]

Element is a language of my own design. For some limited documentation, you can look here. For the source to an Element interpreter written in Perl, you can look here.

Here is a walkthrough of how it works:

2` stores a 2 onto the control stack for later use
116 110 push those values (t and n) onto the stack
[ start a for loop that is repeated twice
   2: duplicate the 110
   1-+ adds a negative one to the top copy of 110
   ' move the top copy to the control stack as temporary storage
   101 add the code for an e onto the stack
   " move the copy from the control stack back on top of the 101 
] end the for loop, this loops takes advantage of the fact that n m l are in consecutive order and both have an e in between 
7' push a 7 onto the control stack
69 push the code for an E onto the stack
[ start a for loop that executes 7 times
   , perform ->char / ->num conversion
   # throws away the top item on the stack since this is the item->num conversion
   ` pop and output the top item on the stack, since this is the needed item->char conversion
] end the for loop, which prints out the numbers in character form in reverse order, to spell "Element".
\$\endgroup\$
0
\$\begingroup\$

F♯ - 187 Char

I had some fun with this - even if it's not really golfed. I should come up with a cleverer way to obscure the 72 and 2 though

let a="It's like OCaml... But .net!"|>Seq.sumBy(fun a->System.Convert.ToInt32(a))
System.Console.Write(System.Convert.ToChar(a%72*2))
System.Console.Write(System.Convert.ToChar(a%72))
\$\endgroup\$
0
\$\begingroup\$

C, obvious golfed solution

main(){putchar(67);}

Of course, it's not quite as short as the J version.

\$\endgroup\$
0
\$\begingroup\$

Java

public class Me {
    public static void main(String[] args) {
            System.out.println(System.class.toString().substring(6, 7).toUpperCase() + System.class.toString().substring(7, 10));
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ It's not a code golf, so we like to see the whole File, please. Does it, by any chance, contain the word Java? \$\endgroup\$ Mar 27, 2012 at 13:19
  • \$\begingroup\$ Looks absolutely fine \$\endgroup\$
    – Armand
    Mar 31, 2012 at 9:07
  • \$\begingroup\$ A working solution. It prints 'Java', extracted from the System class' package declaration, but there's definitely room for improvement regarding code length. \$\endgroup\$
    – codeporn
    Nov 20, 2012 at 9:29
0
\$\begingroup\$

SQL 33 32

print substring(@@version,11, 3)

(tested with SQL Server 2008 R2 SP1)

\$\endgroup\$
0
\$\begingroup\$

Perl - 19

Less perlish than the other perl answer, but shorter.

print substr $^X,-4
\$\endgroup\$
0
\$\begingroup\$

Haskell 137

This uses a fairly interesting method of encoding strings. Thought I'd share it.

import Data.Char
main=
  (print. map chr . reverse . map last . takeWhile (not . null) . iterate (\x->zipWith subtract x$tail x))[50,-14,-31,-20,-7,0,108]

Bonus points to anyone who can explain why this works.

\$\endgroup\$
1
0
\$\begingroup\$

PHP

<?php 
//first option use something that probably isn't available in windows installs
print substr($_SERVER['_'],-3) . "\n";

// this should work everywhere
print strtolower(substr(array_pop(array_filter(array_keys($_SERVER),'filterSELF')),0,3)) . "\n";
// uses the following function to filter out only 'PHP_SELF'
function filterSELF($toCheck){
    return (strlen($toCheck) == 8 && substr($toCheck,-5) == '_SELF');
}

both return php

\$\endgroup\$
0
\$\begingroup\$

Nemerle, 57

Nemerle.IO.print(typeof(Option).FullName.Substring(0,7));
\$\endgroup\$
0
\$\begingroup\$

Python

import sys
try: from StringIO import StringIO
except: from io import StringIO # py3
stdout = sys.stdout
sys.stdout = s = StringIO()
import this # yeah
sys.stdout = stdout
print(s.getvalue()[11:17])
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 36 characters

(subseq(package-name *package*)0 11)
=> "COMMON-LISP"
\$\endgroup\$
0
\$\begingroup\$

Perl

print$^X=~/([a-z]+)$/,$/;
\$\endgroup\$
0
\$\begingroup\$

Rebol

copy/part at system/license 16 5

For Rebol 2 use:

copy/part at system/license 1 5
\$\endgroup\$
0
\$\begingroup\$

Perl, 28

print @a=(a..z)[15,4,17,11];
\$\endgroup\$
0
\$\begingroup\$

Molecule, 2 bytes

;M

Pre-defined variables are awesome.

\$\endgroup\$
2
  • \$\begingroup\$ This ain't no codegolf, son. \$\endgroup\$
    – cat
    Apr 23, 2016 at 17:56
  • \$\begingroup\$ @cat I know. I'm still adding the byte count just because. \$\endgroup\$
    – user47018
    Apr 23, 2016 at 20:16
0
\$\begingroup\$

Factor

Works always:

version-info " " split first print

The hacky way:

(command-line) first "." split >title print

All Factor source files end in .factor, so this just gets that and titlecases it. Won't work for compiled executables, probably.

\$\endgroup\$
-1
\$\begingroup\$

C#, lots of characters

class g
{
    static void Main()
    {
        var r = new System.Diagnostics.StackTrace(new System.Diagnostics.StackFrame(true)).GetFrame(0).GetFileName();
        System.Console.WriteLine(Microsoft.CSharp.CSharpCodeProvider.GetLanguageFromExtension(r.Substring(r.Length - 2, 2)).ToUpper());
    }
}

That's probably closer to the spirit of the problem, but this will also work, hard coding the file extension, and is probably still effectively following the guidelines:

class g
{
    static void Main()
    {
        System.Console.WriteLine(Microsoft.CSharp.CSharpCodeProvider.GetLanguageFromExtension(".cs").ToUpper());
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I read CSharp.CSharp \$\endgroup\$ Mar 27, 2012 at 13:21
-1
\$\begingroup\$

iX2Web

**iX2001C3 934238Mwlp WDJXZWJ8Xz AJMA0K=*
\$\endgroup\$
-1
\$\begingroup\$

Go

This generates an indirect language name, i.e. Issue 9 (hey, it's not directly said - you need to know what this refers to). I think it's a valid name for Go, considering it has redirect on Wikipedia. For more information why Issue 9 is a valid name, please see https://code.google.com/p/go/issues/detail?id=9.

package main

import "fmt"

func main() {
    fmt.Println("Issue 9")
}
\$\endgroup\$
1
  • \$\begingroup\$ This is entertaining. It is a popcon, after all. \$\endgroup\$
    – cat
    Apr 23, 2016 at 17:55
-2
\$\begingroup\$

Java

System.out.println("".getClass().getName().substring(0, 4));

:D

\$\endgroup\$
1
  • \$\begingroup\$ Already given months ago (in a more verbose style). Beside, your code won't compile as is... \$\endgroup\$
    – PhiLho
    Jul 27, 2011 at 15:36
-3
\$\begingroup\$

PHP - 22/14

(assumes a file named 'php')

<?php echo __FILE__;?>

Down to 14 with short tags

<?=__FILE__;?>

And then

php ./php
\$\endgroup\$
2
  • \$\begingroup\$ file name is cheating, surely? \$\endgroup\$
    – Armand
    Mar 31, 2012 at 9:11
  • \$\begingroup\$ Looking at the rules I believe I am well within the guidelines. \$\endgroup\$
    – phatskat
    Mar 31, 2012 at 14:38
-5
\$\begingroup\$

Javascript : 48

alert((function Javascript(){}+"").substr(9,10))
\$\endgroup\$
1
  • 2
    \$\begingroup\$ That contains the test "Javascript." \$\endgroup\$
    – tbodt
    Feb 22, 2014 at 23:12
1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.