8 ms·
If someone asks me of a small snippet that describes some fundamental thing about TCL, I will respond with this: set a puts $a "Hello World" A command
by J-Kuhn 6y ago
If someone asks me of a small snippet that describes some fundamental thing about TCL, I will respond with this:
set a puts
$a "Hello World"
A command is just a list. Substitution is done before executing it. That's why [set] requires the variable name, not $variable.
- deleted 6y ago[deleted]
- adwn 6y agoPython: a = print a("Hello World") Rust: fn puts(s: &str) { println!("{}", s); } let a = puts; a("Hello World"); C++: #include <cstdio> auto a = puts; a("Hello World"); Also: every FP language ever. How is that a fundamental thing about TCL? Also probably all shell scripting languages, like Bash: a=echo $a Hello World
- gecko 6y agoThe way I'm used to seeing the GP's example made is set a pu set b ts $a$b "Hello!" which I think does a better job showing how the mixture of strings with interpolation allows for some slightly unexpected things.
- Twisol 6y agoWould it be fair to call this a kind of "pervasive eval"?
- artemonster 6y agoyou've misspelled "evil" ;)
- adwn 6y agoYes, you're probably right. Frankly, I'd rather not have this kind or error-prone behavior in my programming languages, not even when scripting. It makes proper handling of strings hard and cumbersome (as evidenced by a lot of software that can't handle spaces in file names because it's calling scripts written in sh or bash), and I don't get the Tcl love professed in the comments here.
- setpatchaddress 6y agoI'm not a fan of Tcl -- I used it a bit in the 90's -- but this really isn't the best example. You need a bit more context to give it a fair shake.
- wryoak 6y agoYour attribution of "error prone behavior" to programming languages rather than to their users may explain why you "don't get the Tcl love"
- dleslie 6y agoThis is kind of clumsy in R5RS Scheme: ((eval (string->symbol (string-append "dis" "play"))) "Hello world")
- OJFord 6y agoGood lord, as an example of why it's something we should want? Each to their own of course, but..
- coldtea 6y agoFor example because it's kind of homoiconic. Since everything is a string you can define your own control flow constructs for example...
- adwn 6y agoIf you define your own, say, repeat-until loop, can you use the built-in break and continue commands? Could you define a break-with-value command for the built-in loops?
- dvdkhlng 6y agoUsing Tcl exceptions, I guess it would be pretty trivial to do that. Here [1] is an alternative do..while loop implementation provided by Tcllib, written in pure Tcl. [1] https://core.tcl-lang.org/tcllib/artifact/4f7e97706ff67a40 https://core.tcl-lang.org/tcllib/artifact/4f7e97706ff67a40
- ufo 6y agoI don't think this is the sort of code that people tend to write. But it does a good job of succinctly explaining how Tcl works.
- pgtan 6y agoin TeX: \def\a{mess}\def\b{age}\csname \a\b \endcsname {Hello there}\bye in (UCB)LOGO: ? run list word "pri "nt [Hello there]
- hzhou321 6y agoIn your examples, you need "types" to understand the code. In the parent TCL example, there is no type, thus, way simpler. Programming is a process, not just code and result. TCL process is way simpler.
- rthomas6 6y agoBecause if you say a = "puts" in the other langs it wouldn't work. The other languages are assigning a function to a variable. Tcl is assigning a string to a variable, then evaluating the string as a function based on syntax.
- davidw 6y agoNow do the same thing with 'if' and 'while'. In Tcl, you can define your own do...while loop.
- rsecora 6y agoIf you use quoted strings in both lines, your intent will be more clear. set a "puts" $a "Hello World". On the other hand, I have not used Tcl since the last time I run a Verilog on AIX (21 years ago). I don't miss it.