# An improved version of GolfScript [closed]

I am planning to write an improved GolfScript for even shorter programs that can do more things. This is not a challenge; it is a request for feedback and tips on what I should do. (see tags)

I'm not sure if this should be Community Wiki. If you think so, just flag for a moderator to convert it :)

This language will be very similar to GolfScript. It is still written in Ruby. However, it has a few differences:

• Using  as a string delimiter, because it's an uncommon character, so less escaping will be needed. (Another character can replace its function, like # (more on that later)). \ to escape a backtick, \\ to escape a backslash, and there are no other escape sequences. If you need a newline, just stick an actual literal newline in the string.
• Using Ruby's Rationals for arbitrary precision floating point, one of GolfScript's major flaws.
• The ability to convert types to other types. For example, you can convert a block to a string.
• Regular expressions. Probably created with "...". Operators will be overloaded for them as well. For example, "\W"~{Invalid: non-word character}{OK}if. Will automatically execute when pushed from a variable, like blocks.
• File and Date objects, to do more stuff that was impossible in GolfScript. These will not have literals, but will have functions for initializing them, such as file.txtfl (name of the file-making-function may change).
• Hashes maybe, but I'm not sure on that one. Should I?
• Helper functions to do even more. For example, http://example.comnet for network access (again, net operator may be renamed). rb to execute a string as Ruby code. There will be many more of these; suggestions welcome.
• No comments, so that # can be used for something else. If you want a comment, comment here; will work fine. (Maybe # can replace 's function)
• It will be completely rewritten in a way that it will be much easier to add functions. Basically, the code will be more readable. (Have you seen the GolfScript source? :/)
• It will be on Github so it can be collaboratively worked on. I'll license it under MIT or something.
• No final newline, so cheaty quines work :P

And I'm setting these apart because I think they're the most drastic and helpful changes (except maybe adding floating point):

• It will have many Ruby functions built-in. For example, shuffle (which may be abbreviated to sf) (previously took 9 characters), tr (previously 14 chars), sample (sm, previously .,rand=), flatten (fl, previously ???), etc.
• It will be mushed, like Rebmu. For example, now you can do ~:a0<{0a-}aIF (using a letter variable name) instead of ~:$0<{0$-}$if (overwriting the sort function). (example from here). Note that this way, it is case-insensitive, and numbers are not allowed in variable names. This is okay in my opinion since it's a golfing language :P • It will have debugging. I will add the ability to supply a flag specifying array delimiters, element delimiters, etc., number output (rational, float, or int?), stepping through instructions one at a time, tokenizing and outputting each token instead of running the program, etc. So, my question is: what is there to improve? What do you think I should add? Any other ideas for this, before I start coding it? • Do realize that if you add mushing you lose case sensitivity. – marinus Dec 31 '13 at 19:03 • @marinus I realize that. I should have made that clear; editing... – Doorknob Dec 31 '13 at 19:04 • Also what it really needs is a way to do explicit I/O (for interactive programs). The interpreter could check beforehand if the program uses explicit I/O, and only do implicit I/O if it doesn't. – marinus Dec 31 '13 at 19:05 • @marinus Great, you should add that as an answer then :) – Doorknob Dec 31 '13 at 19:06 • @Claudiu My development on my new language is quite alive, although I've recently been fairly busy so I haven't done much with it for a little while. Some of these ideas are just not good ideas though, which I am not using :P – Doorknob Mar 30 '14 at 3:01 ## 23 Answers ## Flexible I/O Golfscript cannot at present be used for interactive programs. I propose some functions for explicit input be added (i.e. readline, getchar and friends). The interpreter should see if the program uses these before running it. If the program does not call any input functions, the interpreter should act like Golfscript normally does. I wouldn't expect the interpreter to detect input functions in eval'ed code generated at runtime, but if it somehow can do that, kudos. • This is a great idea; limited io is one of golfscript's large limitations. +1 – Doorknob Dec 31 '13 at 19:16 ## Shorter built-ins Single-character aliases for all of the built-in commands which don't have them. I would use base a lot more if it were just B. • But what if you use b as a variable name? Still; good idea; you just remember to not use that name if you're going to use the function, and if you aren't using the function, then it doesn't affect you at all. – Doorknob Dec 31 '13 at 19:24 • @DoorknobofSnow, you can already use built-ins (and even tokens such as ^ or $) as variable names. It doesn't make that problem any worse. Besides, I suggested aliases to allow backwards compatibility, so you would just have to use the longer name if you'd assigned something else to the shorter alias. – Peter Taylor Dec 31 '13 at 19:26
• Z for zip would also be very useful. – Howard Dec 31 '13 at 19:29
• Maybe we can release an official standard library for GolfScript. But I am unclear about how it can be included in solutions (missing #include and "#{IO.read'lib'}"~ is too long). – Howard Jan 1 '14 at 7:41
• @Howard Now that's an idea... I could add libraries into my language, maybe like mylang -Llibname somefile.ext. – Doorknob Jan 1 '14 at 13:39

## Combined div-mod

This is a bit more niche than some of the suggestions, but when working on number-theoretic programs I frequently find myself wanting an operation which pops two integers a and b from the stack and pushes a/b and a%b. (At present this is 1$1$/@@%).

• Since it's so niche, maybe something like dvm for DiV-Mod. Thanks for all the ideas :-) +1 – Doorknob Dec 31 '13 at 23:52

### Numbers

Change the lexer such that leading 0 is not part of a number:

# current behaviour
01     # -> 1

# new
01     # -> 0 1


Also negative numbers should be written with _ instead:

# current behaviour
1 2-3   # -> -1 3

# new
1 2_3     # -> 1 2 -3

• Wow, that's interesting. Usually you don't typically push two numbers in a row, but this would help with that a lot. The negative thing is great too, instead of 0 100- for negative 100. +1 – Doorknob Dec 31 '13 at 19:30
• I've thought about this before. There shouldn't be any support for negative integer literals at all. It's already handled by ~. E.g. -1 is 0~. This makes a small number of numbers longer by one char, but removes the moderately frequent need for whitespace after -. – Peter Taylor Dec 31 '13 at 19:33
• Wait a minute, how would you push, for example, 1001? I think you meant leading zero, not trailing. – Doorknob Dec 31 '13 at 19:33
• @DoorknobofSnow Right. – Howard Dec 31 '13 at 19:39
• What would you think about doing {0\-}:~; (~ negative-izes a number) and using not for bitwise (just like and or xor)? – Doorknob Jan 1 '14 at 1:22

GolfScript is a stack-based language, but access to all but the top three items on the stack is limited to <integer>$ to copy the nth item. It would be useful to have something like PostScript's roll command so that it's easier to work with more than three "live" variables. Ideally there would be one-arg and two-arg versions, but if there aren't enough names around then the one-arg should get preference for a one-character one. The one-arg one just takes the number of items to roll. E.g. 1 roll does nothing; 2 roll is equivalent to \; 3 roll is equivalent to @; 4 roll and for higher numbers doesn't have an existing equivalent; the closest that's possible is something like ]-4/()\+\+[]*-1%~  (and that doesn't even handle non-integers at certain positions on the stack, or active [, and almost certainly breaks inside loops too). The two-arg one also takes an amount to roll; a b roll2 is equivalent to {a roll}b*. • So, like Ruby's rotate. You should edit this into the CW answer. – Doorknob Dec 31 '13 at 23:54 • @Doorknob, no. I'm talking about operating on a slice of the stack, not on an array. – Peter Taylor Jan 1 '14 at 8:41 • Umm...I'm not sure what you mean. I'm not familiar with postscript, but roll just rotates the array, right? – Doorknob Jan 1 '14 at 13:37 • @DoorknobofSnow, stack. An array is an item which goes on the stack. – Peter Taylor Jan 1 '14 at 14:10 • Oh, so it rotates the whole stack. Ah, that might be useful! +1 – Doorknob Jan 1 '14 at 14:32 # CJam I have implemented "an improved GolfScript" and it is called CJam - http://sf.net/p/cjam Now at the second release (version 0.6) it already has many if not most of the features discussed here. I'll try to list them: • still written in Ruby - nope, java • using  as a string delimiter - no, but it uses double-quoted strings with minimal escaping (\ escapes only \ and ") • floating point - supported, but only standard "double", not arbitrary precision • convert types to other types - yes • regular expressions - not yet, but planned; will use regular strings with special operators • File and Date objects - no, but can get the current date/time • hashes - assuming those are like python dicts or java maps, then they're not supported (may consider in the future) • helper functions to do even more - yes, a lot • http://example.comnet - "example.com"g • execute a string as Ruby code - nope • no comments - exactly, # used for something else, "comments like this"; • easier to add functions - I think so, but I'm also biased :) • on Github - even better (in my opinion, please don't shoot) - on SourceForge, using hg • licensed under MIT - yes • no final newline - right • shuffle - mr • tr - er • sample - not done, _,mr= • flatten - not done, but probably easier to achieve • mushed - nope, but identifiers don't need to be separated • debugging - only stack traces, and ed operator for showing the stack • flexible I/O - yes, but only explicit input • shorter built-ins - yes, b = base, z = zip • separate leading 0 - no, but can use predefined variables • disambiguate - - yes, but not with _; 1 2-3 -> 1 2 -3; 1 2m3 -> -1 3 • roll/rotate the stack - nope • array set - t • divmod - md • change the lexer (for identifiers) - yes, more below • cartesian product - not exactly the same, but yes, m* • unicode operators - no • single-character identifiers - predefined operators have 1 or 2 characters and variables are single-character uppercase letters; they can all be concatenated without confusing the lexer/parser • operators on blocks - no • stable sort - yes • turn symbols back into code blocks - no, but may add later • current date/time - et • command line args - ea • clearly separating built-ins - yes, but capitals are variables; built-ins are or start with lowercase and special characters • min and max - yes, currently only for 2 values: e<, e> • absolute value - z (GolfScript has abs, not lacking) • sum and product of an array - :+, :* • Manhattan distance - nope • chr - c (converts to a character, not a string) • spill a string onto the stack - CJam strings are made of characters, not numbers; to spill the characters it's still {}/ • a version of : that consumes what is stored - nope • operators for >=, <= - nope, use <!, >! • base64 and zlib - nope • shortcuts for 1$, 2$, 3$, 4$, 5$ - nope
• copy the top two stack items - planned; for now use 1$1$
• local variables - nope
• HQ9+ features - no, thank you

CJam has a lot more features, check out https://sourceforge.net/p/cjam/wiki/Operators/

• I disagree with SourceForge and Mercurial being better than GitHub. – nyuszika7h May 2 '14 at 10:44
• @nyuszika7h it's ok, we all have our preferences – aditsu May 2 '14 at 11:19
• I just posted my very first CJam answer, because my GolfScript answer was in desperate need of a proper sqrt function. CJam is a lot faster than GolfScript! – Dennis May 22 '14 at 0:38
• @Dennis Awesome :) – aditsu May 22 '14 at 1:03
• @aditsu Would you consider CJam a dialect of GolfScript or a new language inspired by GolfScript? (Here's the reason I'm asking: codegolf.stackexchange.com/questions/37464/…) – Martin Ender Sep 15 '14 at 15:07

## Change the lexer

GolfScript's lexer treats a Ruby identifier (anything which matches the regex [_a-zA-Z][_a-zA-Z0-9]*) as a single token. If it instead treated [a-zA-Z]+ as a token that would free up _ to be a built-in and would allow an alpha variable to be followed by a literal integer without separating whitespace.

• Well, currently I'm using simply [a-z]+|[A-Z]+, for the mushing, so underscore is free. This is an interesting and very unique idea though! +1 – Doorknob Dec 31 '13 at 19:27

## Unicode aliases

Multiple-character commands could have unicode aliases. This would save on the score when the score is counted in characters and not in bytes.

• uh... we don't want another APL... – John Dvorak Dec 31 '13 at 19:40
• Golfscript isn't anything like APL, Unicode or no. We already have several other APLs, like J and K. (or even R which looks quite APL-inspired to me, to the point that you can do literal translations quite often) – marinus Dec 31 '13 at 19:54
• The distinguishing point about APL is that it uses a very different charset than anything else, not that it uses vector arithmetic natively. I like J, I use it, and I commonly upvote J answers. I've yet to upvote one APL answer. If I wanted to paint my programs instead of writing them, I'd use Piet. – John Dvorak Dec 31 '13 at 19:57
• I think APL is more legible than J, certainly more legible than Golfscript. – marinus Dec 31 '13 at 20:06

# More built-in functions

Make all the single-letter variables a-z and A-Z perform some generic, useful function. Some built-ins that are lacking:

• min and max: all or some of top 2 stack values, top n stack values, over an array
• absolute value
• sum and product of an array. why do {+}* when you can do S? You have 52 functions to work with here!
• Manhattan distance (i.e. x1 y1 x2 y2 --> abs(x2-x1)+abs(y2-y1). Now it'd have to be @-A@@-A+ if A is a built-in absolute value. Granted this only came up caues of my most recent post but I always figured that'd be a good way to expand golfscript: write down what functions would be handy to have, collect them, and add them as built-ins.
• Convert an integer to a one-character string (equivalent of python's chr).
• Spill a string onto the stack (currently {}/)
• A version of : that consumes what is stored. This would have to not get 'stuck' to identifiers to be useful.
• Operators for >=, <=
• As someone suggested, a way to put a variable containing a block onto the stack without executing it. So you could reduce ifs of the form 1{\}{|}if to something like 1?\?|if
• Built-in base64 conversion and zlib support to make embedding data take less characters
• Beyond base64, make a custom base93 encoding (using all printable characters that aren't the string delimiter).
• Shortcuts for 1$, 2$, 3$, 4$, 5\$
• An operator to copy the top two stack items as they are, i.e. \.@.@\

It'd be nice if the value written or computed in the last line of a function was automatically returned

• It's stack-based, so... – marinus Dec 31 '13 at 19:30
• There's no such thing as a function. – Peter Taylor Dec 31 '13 at 19:31
• Functions don't return anything... and they're called blocks – Doorknob Dec 31 '13 at 19:32
• That might be nice...in some other language. (No more annoying return statements at the end of functions! Yay! (But I haven't needed return statements ever, so...)) – CalculatorFeline Mar 7 '16 at 3:26