14 ms·
Why is Tcl syntax so weird (2013)
- ljm 5y agofor {set sum 0; set counter 0} {$counter < 10} {incr sum $counter; incr counter} {} It's amusing how that pattern was adopted in Javascript (well, C/Java really I guess) as an optimisation. for (var sum = 0, counter = 0; counter < 10; sum = counter++) {}
- Koshkin 5y agoBut your JS code has a bug in it while the Tcl code does not.
- maybe_pablo 5y agoI find this Tcl quine quite amazing: join {{} \{ \}} {join {{} \{ \}} } I think it tells a lot about the weirdness and awesomeness of the language at the same time.
- jrapdx3 5y agoThanks for that great puzzle! Took me a few minutes to figure out what was going on with it. Indeed very clever--and amusing. I concur Tcl is often awesome and at times also terribly irksome. I suppose the same could be said of Lisps in general. (Like many others I'd say Tcl belongs in the family of Lisp and its direct descendants.)
- int_19h 5y agoWhile we're at it - Tcl-like languages make for pretty decent embedded scripting solutions if performance is not a major requirement. There's one called LIL that's literally just a single .c file (and the accompanying header) for ease of embedding: http://runtimeterror.com/tech/lil/ http://runtimeterror.com/tech/lil/
- pmarin 5y agoMy favorite implementations are partcl[1] (600 lines) and Picol[2] (500 lines by Salvatore Sanfilippo). [1] https://github.com/zserge/partcl https://github.com/zserge/partcl [2] http://oldblog.antirez.com/post/picol.html http://oldblog.antirez.com/post/picol.html
- int_19h 5y agoSo far as I can tell, the reason why they're so small is because they implement "everything is a string" literally - so e.g. appending to a list is basically concat. This is good enough as a proof of concept, and neatly showcases just how simple the language itself really is; but perf would be abysmal even by Tcl standards as soon as you get more than a few dozen items per list. LIL tries to be a minimalist but practical embeddable implementation, so it has basic optimizations like representing lists more efficiently internally. The price, of course, is code size, but also C API complexity.
- deleted 5y ago[deleted]
- tracnar 5y agoThe Tcl wiki is quite strange, it has interesting content but most pages are discussions like this one and it's hard to find what's relevant or even correct. What's weird about the Tcl syntax is that there is barely any. It's very dynamic so only the top-level of a file is parseable as a list of commands, anything else like a procedure depends on the implementation of the command which can be overridden at any time. It's even used by built-in commands like expr and if which essentially implement a mini expression language which is different from Tcl.
- kevin_thibedeau 5y agoThat's what wikis originally were. Wikipedia's curated ontology is an exception that only looks like the rule because most OG wikis have died off.
- emodendroket 5y agoEmacswiki is one active one still like that and the effort to replace it with MediaWiki was ultimately abortive. C2 is also big but I think mostly dead.
- lifthrasiir 5y agoThat is true but doesn't fully explain the OP's "what's relevant or even correct" bit. The truth is that OG wikis needed a special kind of moderators---affectionally called WikiGardener or even WikiMaster---who clean up things and deal with conflicts without removing values provided by prior authors but possibly removing the contents themselves. If this sounds absurd that's because it does; it is waaay harder to maintain than typical forums and even encyclopedic wikis popularized by Wikipedia. And without those kind of hard and thankless moderation non-encyclopedic wikis generally end up with being a dump of uncurated informations.
- natrys 5y agoI have been writing some Tcl for the last couple of years, mostly in place of shell scripts. Going to Tcl wiki is always a surreal experience. I think this old comment captured it perfectly: https://news.ycombinator.com/item?id=9098859 https://news.ycombinator.com/item?id=9098859
- lilyball 5y agoI’ve always loved Tcl because of its absolute purity and simplicity at the language level. I love how you can implement your own control flow operators that look like built-in ones. I love how easily you can make DSLs. I just wish there was a Tcl that was redesigned from a modern perspective. Give me first-class lambdas. Give me unnamed objects that get destroyed automatically (with a callback) when out of scope[1] so I don’t have to use a weird namespace hack to make the object and rely on the caller to invoke the destroy method. Give me a stdlib that offers more functional stuff. Heck, give me async/await. And more too. But even without all of that, Tcl is still a wonderful language and I often lament how it’s mostly died out. [1] There’s at least one library I’ve used that actually does this, but AFAIK there’s no way to do this in pure Tcl so it must be a feature of the C API. Also I’m not sure if it actually tracks the object or just the variable it’s assigned to.
- valenterry 5y agoCheckout Scala. It allows you to do pretty much the same, i.e. the syntax is so flexible that you can even make 'puts "Hello World!"' work and implement your own "if" that looks like the builtin one. It also has first-class lambdas and the other stuff that you asked for, but obviously there are also differences.
- lilyball 5y agoScala runs on the JVM, which makes it not at all suited for scripting. Scala also seems to have something of a racism/white supremacy/misogyny problem.
- deleted 5y ago[deleted]
- celeritascelery 5y ago> Tcl is a macro language, it derives from macro-assemblers, the C preprocessor, and older, text-oriented languages like TRAC Then why doesn’t it have macros? This is one of my biggest complaints with tcl, the other being that you have to escape newlines in forms. This makes much less pleasant then lisp. For example if I have this Tcl code [set foo [foofn bar [barfn baz [bazfn quz 1]]]] I would love to write it like this [set foo [foofn bar [barfn baz [bazfn quz 1]]]] But I can’t without escaping every newline (you better hope you didn’t forget one). If tcl had macros I could write it like this [set foo [thread-last 1 [bazfn quz] [barfn baz] [foofn bar]] But instead I end up create a bunch of temporary variables so that my lines don’t get super long. It just feels less elegant then it could have been.
- mjevans 5y agoI dislike that too about most of the languages which are in some way white-space sensitive. (Python as the go-to worst example, but other modern languages where ; or a statement termination value is optional also fit.) Newlines only can work, but then the question revolves around implicit newline escapes (if there's an unclosed statement) versus a required escape and things can quickly get out of hand and readability if really long strings need to be passed around; like with nightmare long SQL queries or default / example config file blocks.
- tyingq 5y agoIt's a bit odd, because you can skip newlines in many cases. This works: #!/usr/bin/tclsh set foo [ lindex [ expr 2 * 3 ] 0 ] puts $foo
- theamk 5y agoTCL does not need macros because every function can be a macro. Here is an answer to your question: proc multiline {expr} {uplevel [regsub "\n" $expr " "]} Use it as following: multiline { set foo [ foofn bar [ barfn baz [ bazfn quz 1 ]]] } try it in action: https://jdoodle.com/ia/jka https://jdoodle.com/ia/jka The hardest part to understand when writing TCL "macros", is that it's all strings. You might be used to "inputs are s-expressions" from lisp, or "inputs are closure objects with pointers to code and local variables", but not in TCL. {} produces a string parameter. It is interpreted as an expression to be evaluated in parent context when passed to "uplevel". So "proc" takes 3 strings as arguments, and syntactically no different from "regexp".
- ahartmetz 5y agoThe CMake language has the same absence of syntax, and everything is a command as well - like assignments with set(). Oh, and all variables are strings, too like in Tcl. (In CMake, even lists of strings are strings. Don't ask.) The CMake language was created in a haphazard way as well, with some other important problems that Tcl probably doesn't have.
- Y_Y 5y agoCMake is the modern MUMPS.
- bch 5y ago> The CMake language has the same absence of syntax Ironically, if I recall correctly, cmake came about from a Kitware medical imaging project that was heavily leaning on Tcl, yet when their build system needed a DSL they decided to hack something up from scratch :/
- AnonC 5y agoI’ve seen Tcl used to write testing systems a long time ago, but never really worked with it. Tangentially, I found that the primary author of SQLite and Fossil SCM, D Richard Hipp, was also a core team member of Tcl. The server side of Fossil uses a minimal dialect of Tcl and the build/test system is built with Tcl. [1] [1]: https://fossil-scm.org/home/doc/trunk/www/fossil-v-git.wiki https://fossil-scm.org/home/doc/trunk/www/fossil-v-git.wiki
- denton-scratch 5y agoArticle doesn't do a good job of explaining why Tcl syntax isn't weird. I had to code in it once, can't remember what, something trivial; but I had to learn Tcl. It was basically an obstacle to my work. I never needed to use it again, so I've forgotten it, so I wasted my time learning that language.
- cmacleod4 5y agoI wrote a blog post last year trying to explain the under-appreciated radical minimalism of Tcl, and in particular the decoupling of syntax and semantics - https://colin-macleod.blogspot.com/2020/10/why-im-tcl-ish.html https://colin-macleod.blogspot.com/2020/10/why-im-tcl-ish.ht...
- marttt 5y agoGreat read, thanks! It also received 100+ comments on HN: https://news.ycombinator.com/item?id=24897326 https://news.ycombinator.com/item?id=24897326
- Animats 5y agoTcl is too much like shell script programming. It's one of those "take one basic concept and use it for everything" systems. Could be worse. Could be Forth.
- dmux 5y ago>Tcl is too much like shell script programming. How so? Tcl has namespaces, a standard lib, runtime introspection, an OO system, threads, asynchronous programming capabilities via event loop, a GUI framework and is cross platform. Have you actually used it?
- sigzero 5y agoI love Tcl. The community is awesome as well.
- johnisgood 5y agoWho remembers Eggdrop, the IRC bot? https://en.wikipedia.org/wiki/Eggdrop https://en.wikipedia.org/wiki/Eggdrop https://www.eggheads.org https://www.eggheads.org https://www.tclarchive.org https://www.tclarchive.org (Blast from the past!) I was surprised to see that it is still under development. After Eggdrop, it was Supybot that got really popular, although it is written in Python.
- zquestz 5y agoFigured this thread would be full of eggdrop comments. So many good memories writing TCL scripts back in the 90's. Really happy to see the project is still developed, definitely a part of internet history for me.
- johnisgood 5y agoYeah! I was surprised to see it is still under development after so long! Pleasant memories tied to all of it. :)
- Fuzzwah 5y agoI clicked through to these comments expecting that it would be full of discussion about eggdrop bots, as that was where I experienced tcl. To learn that it is heavily used at places such as Intel for tooling controls was very interesting to me. I still maintain a bot that began life as a mirc script, then a long time as an eggdrop bot, then ported to python based slackbot, and now most recently a python based discord bot. Over the last 25 years it really has been a huge part of the way my group of friends interact. I recall the sheer wonder that I experienced when I learned about eggdrop bots and the huge amount that I learned while flailing around with tcl.
- johnisgood 5y ago> To learn that it is heavily used at places such as Intel for tooling controls was very interesting to me. Same! It is quite interesting, I have not heard of it before. My first experience with Tcl was through Eggdrop. I was really young at the time! I remember those mIRC scripts and different mIRC clients or something, they may have been through plugins? I don't recall, but I remember after installing them my mIRC looked very different and so on. After Eggdrop came Supybot which was really popular at the time, then after that I just made my own. I have a Discord bot as well, I made it myself, although it is written in Lua because I made it around the time I was tinkering around with the language. A more recent experience with Tcl was modifying Tclsh to make it suitable to be used as a replacement for bash. It works quite well now, I would say. Actually, a friend of mine did most of the job but still!
- juxtapose 5y agoTcl is a better shell scripting language. Tcl's escaping is simple and consistent, while bash is almost impossible to grok -- just too many implicit rules to fit in my head.
- bschwindHN 5y agoMy most miserable programming experience was with Tcl. It was used as a plugin language for a heat transfer application. Maybe it allows for some "clever" code or "powerful" concepts but for its use in that application, it was just not straightforward. It was used among a pretty large team and it just felt so messy and chaotic. At that scale I would want type checking and the ability to not have to think "okay is this a variable or is it getting interpolated in some other code, or what?"
- lmm 5y agoAs embeddable scripting languages go it's pretty straightforward IME? I agree that you probably want a safer language for building large systems (Tcl is significantly older than OCaml, which I'd argue is the language that popularised non-cumbersome type systems), but considered as a language in the Perl/Python/Ruby/Lua kind of space, Tcl is reasonably clean IMO.
- johnisgood 5y agoDo any of you use tclsh as your shell?
- cmacleod4 5y agoTclsh is not really suitable for use directly as a general-purpose shell. TkCon gets a bit closer - https://wiki.tcl-lang.org/page/Tkcon https://wiki.tcl-lang.org/page/Tkcon - but still not great, e.g. if you start a long-running program you will only see its output when it terminates. I wrote a semi-graphical general-purpose shell in Tcl/Tk for my own use - https://wiki.tcl-lang.org/page/gush https://wiki.tcl-lang.org/page/gush . I used this as my main shell at work for 12 years, and only stopped because I retired last year.