12
\$\begingroup\$

GolfScript automatically coerces values, according to the following ‘priority’ sequence:

integer → array → string → block

(listed from lowest to highest.)

This means that "1"7+ leaves the string "17" on the stack (and not 8), because strings have a higher priority than integers.

[To those GolfScript veterans – apologies for insulting your respective intelligences.]

How would it be possible to do this the other way? That is, to coerce a string into an integer?

\$\endgroup\$
5
  • \$\begingroup\$ name:~ args:1 functions:bitwise not, dump, eval \$\endgroup\$ Commented Jul 16, 2016 at 18:59
  • \$\begingroup\$ This gives 8. \$\endgroup\$ Commented Jul 16, 2016 at 19:00
  • \$\begingroup\$ @LeakyNun; Nice! Do you want to want to put this in an answer? I'd certainly accept it. \$\endgroup\$ Commented Jul 16, 2016 at 19:02
  • 6
    \$\begingroup\$ @closevoter Questions asking tips about golfing code are on-topic. \$\endgroup\$ Commented Jul 16, 2016 at 19:10
  • \$\begingroup\$ @downvoter; could you please leave a comment explaining why you have downvoted? \$\endgroup\$ Commented Jul 18, 2016 at 16:45

1 Answer 1

10
\$\begingroup\$

There's an operator ~ in golfscript, which does the following: bitwise not, dump, eval.

Therefore, "1" will give the string "1" while "1"~ will evaluate that string (into the integer 1).

Then, all you need to do is "1"~7+.

Try it online!

\$\endgroup\$
1
  • 5
    \$\begingroup\$ It's worth mentioning that this is an eval of golfscript code, meaning e.g. "1.0"~ does something unexpected. \$\endgroup\$ Commented Jul 17, 2016 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.