9
\$\begingroup\$

For a computer language, a reserved word is a word that cannot be used as an identifier, such as the name of a variable, function, or label. For other computer languages, keywords can be considered as the set of the language instructions.

Challenge

Using your language of choice, write a code in the chosen language that given a number between one and ten, 1<=n<=10, outputs any n reserved words (keywords) of the chosen language.

Specifics

  • If the chosen language is case sensitive the outputted keywords must be also.
  • If the chosen language is not case sensitive the outputted keywords can be in any case.
  • If the chosen language has less than 10 keywords saying p, the code must output all the reserved words for any n between p and 10.
  • If possible specify in the answer whether you consider operators as keywords or not.

Possible samples for Java (JDK10)

  • n=1 --> true
  • n=3 --> try new interface
  • n=4 --> continue this long break

Possible samples for ><>

  • n=1 --> >
  • n=3 --> > < ^
  • n=4 --> > < \ /

Possible samples for Brain-Flak

  • n=1 --> (
  • n=3 --> ( ) [ ]
  • n=9 --> ( ) [ ] { } < >

Rules

  • The input and output can be given in any convenient format.
  • No need to handle invalid input values, valid inputs are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
  • Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
  • If possible, please include a link to an on-line testing environment so other people can try out your code!
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Mego Apr 19 '18 at 2:29
  • 2
    \$\begingroup\$ continue this long break I wish! That's why I'm on SE! \$\endgroup\$ – Stan Strum Apr 24 '18 at 18:09
  • \$\begingroup\$ the integers are reserved but I guess that would be a loophole. \$\endgroup\$ – snoram Aug 2 '18 at 13:56

33 Answers 33

1
2
1
\$\begingroup\$

MATL, 5 bytes

:96+c

Try it online!

The lowercase alphabets all correspond to builtins in MATL, so this outputs the first n lowercase letters.

For a version with a smile, there's:

2Y2i:)

at 6 bytes.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Julia 0.6, 59 bytes

n->split("if do for try let end type else true macro")[1:n]

Try it online!

Using the list of reserved keywords from julia-parser.scm.

An operators-included version gets quickly confusing, as to what is an operator and what is a syntactic indicator (like @ for a macro). Going by julia-parser.scm again, a bunch of operators are termed "syntactic operators" and can't be assigned to or overloaded by the user (so fit the definition of "reserved"). Some of these are:

Julia 0.6, 45 bytes

n->split("= : :: && || ... >: <: -> .=")[1:n]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Cubix, 12 bytes

Based on some of the other answers I think this should qualify for reserved words. I avoided using the numeric literals as I thought that might be stretching the definition a bit far.

SoI;W(>!@+/$

Try it online!

This wraps onto the cube as follows

    S o
    I ;
W ( > ! @ + / $
. . . . . . . .
    . .
    . .

Watch it run

  • WSI change lane, push 32 and get input. This sets up the stack with 32 and the input value.
  • >!@ redirect, test for 0 and halt if true.
  • +/o; add stack items, reflect onto top face, output character and pop the sum.
  • !/$W( redundant test, reflect, skip the lane change, decrement the input and back into the main loop.

Output for 10

*)('&%$#"!

* multiply the top two items on the stack
) increment the TOS value
( decrement the TOS value
' push the next character's value to the stack
& pop top two items (integers) of the stack, concatenate and push the int result
% take modulo of top two stack items
$ Skip the next command
# push the stack length
" start and end of string literal.  Character codes of string are pushed to the stack
! test for truthy and skip next command if true
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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