Live data from Hacker News

Giving Ada a Chance

ajxs.me

211–220 of 261 posts

Re: Giving Ada a Chance

#211
post #154

Earlier quoted context omitted.

One area where I think Ada has the edge is providing language constructs that make bare metal programming safer. Concepts like 'dangling pointers' and 'memory leaks' aren't relevant in a programming environment without a heap. In bare-metal programming on a microcontroller you're more likely working within a flat memory model where the 'memory safety' provided by some modern programming languages is less relevant. Ar…

You can absolutely cause a pointer to dangle without heap allocation. Pointers can point to the stack too. You also have stuff like iterator invalidation, which is sort of a special case of a dangling pointer.

I do bare metal programming. I have a macro + linker widget called stack_allocated_ptr(x)

So I can write guards like

   if(stack_allocated_ptr(thingie))
   { 
     exit_critical_error( "oopies");
   }

Re: Giving Ada a Chance

#212
post #39

> I can’t help but think that complicated programming paradigms would seem more intuitive to beginners if taught through Ada instead of C and its derivative languages, as is common in computer science and engineering faculties worldwide. At my university, the first courses you took in CS used Ada. I think it was a really good choice but I was in the minority I guess because after my year they switched to either using…

There's a lot of Python dislike on here, so I thought I'd add my anecdotal story. I learned Basic, C, Assembly, and Matlab in college (in that order). However, I was never a very good programmer. After graduating, I bought an intro to Python book and read it cover to cover and did the examples. I then started writing scripts and it all kind of clicked. I found it really simple to build stuff. There's lists, tuples, d…

I started the other way round and came to Python long after I coded a lot in statically typed languages like Object Pascal, C++, Java, Scala and Rust. My feelings about Python are quite opposite to yours. I'm actually quite surprised how clunky Python is from my perspective.

I expected a small, simple beginner-friendly language, optimized for gluing stuff together, but I've found a huge number of overlapping features and idioms, more ways to do the same thing than in Scala, half-broken libraries with poor inline documentation, lot of "stringly" typed code everywhere where programmers don't seem to know other types than strings, ints and dicts, no ADTs / pattern matching, package version conflicts and all that with no help from the type system and unreliable help from IDE autocomplete.

Type annotations help a bit, but they are still far behind what's available in modern statically typed languages.

I've also run into a few things that were weirdly complex to do compared to other languages - e.g sending rest requests in parallel. Something I'd expect a glue language shine at.

It just feels like a major step backwards at least vs Scala and Rust which I used most recently.

So maybe it is a matter of earlier experience, familiarity and expectations?

Re: Giving Ada a Chance

#213
post #202

Earlier quoted context omitted.

It's not merely that. That for-loops work by assignment rather than creating a new scope, or that if-conditions do not create a new scope for their arms is most unusual. Not only does it not teach programmers to properly reason about scope, but it results into subtle bugs that are easy to miss. Consider the following: list = [] for x in iterator: list.append(lambda y: some_code_that_closes_over(x)) This almost certai…

All you need is "x=x" to make a copy: list = [] for x in iterator: list.append(lambda y, x=x: some_code_that_closes_over(x)) That way the closure is explicit and much clearer when you look at it later. ("list" is a builtin, so not a good variable name.)

> All you need is "x=x" to make a copy:

Your code is illegal Python on two levels.

- Lambdas in Python do not allow assignment at all.

- Variables in Python cannot be assigned using themselves in their r.h.s., without first being assigned something else, because Python's scoping is strange.

You serve well as an example of a programmer that does not understand Python's semantics here, because they are very counter-intuitive, and you also serve as an example of a programmer that does not understand how scope works at all, even if your example be worked into something of valid Python syntax:

  thunks = []
  for x in "abcd":
    def thunk():
      y = x
      print(y, end='')
    thunks.append(thunk)

  for thunk in thunks:
    thunk()
The output is `dddd`, not `abcd`; the `y = x` part is completely irrelevant in this case, for when the thunk is called, `x` has already been re-assigned, and so `y = x`, is assigned the new value.

Again, `x` is re-assigned on every new iteration of the loop, as such the `x` in every single one of those thunks contains the value `x` had at the last iteration of the loop when the loop completes.

There are many ways to solve this issue, such as the one I initially gave, but yours isn't one of them and that you thought it was shows the counter-intuitive nature of Python's behavior here.

> ("list" is a builtin, so not a good variable name.)

Which would be another problem with Python's lack of scope. Shadowing the names of library functions and constants is not problematic in languages with proper block scope.

Re: Giving Ada a Chance

#214

Earlier quoted context omitted.

I help beginner python students. Python might be a nice easy scripting language for bashing out NUMPY scripts, but I'm beginning to suspect it is terrible for teaching. The "what type is this variable, and will this function automatically convert it for me" game is not very fun at all for beginners.

Beginners to programming don't agonize over variable types, they don't have the mental model of statically typed language in their head.

They may not have "a mental model of statically typed languages," but they sure "agonize" when there are eight different incompatible datatypes that represent a datetime and the library functions they want to use don't even specify which of these they take or return.

Re: Giving Ada a Chance

#215
post #201

Earlier quoted context omitted.

No, the first example illustrates the fundamental problem that all iterations of the loop share the same scope rather than a new one each. In your example, the variable `x` is also shared with all iterations of the loop. Rather, it is more so as so in C , like syntax what is the common approach: while(1) { int x = next(iterator) if(STOPITER) { break } /* code that uses x */ } Every iteration of the loop receives a br…

I would argue that code that is sensitive to scope is bad code no matter which language it's written in, since now you're constantly asking "When does a variable leave scope." If I change the scope of a variable, it causes a subtle change that breaks the closure. In that case I would prefer to be explicit that the closure "wraps" a new instance of the variable rather than relying upon implicit language behavior to gu…

> I would argue that code that is sensitive to scope is bad code no matter which language it's written in

Then you have argued that using any form of functions or subroutines in any language is bad design.

> since now you're constantly asking "When does a variable leave scope." If I change the scope of a variable, it causes a subtle change that breaks the closure.

Yes, that is what one must ask oneself as a programmer and that is what programmers who have not been taught wrong practices by having used Python as their introduction are instinctively constantly asking themselves.

Python programmers must also ask themselves this whenever they use functions and Python comes with a variety of ugly hacks around it's initial wanton design by a programmer who clearly does not understand scope how he originally designed it such as `nonlocal`.

Inside any function in Python there are four kinds of variables in terms of their scope: normal, formal, global, and nonlocal, each of them has a different scope and the programmer best be mindful of which is which as he codes, because they have widely different semantics and because Python has the same syntax for assignment and initialization and this difference especially creates very different interpretations for those four types.

> In that case I would prefer to be explicit that the closure "wraps" a new instance of the variable rather than relying upon implicit language behavior to guarantee the scope is correct.

I feel that you have a fundamental misunderstanding of what a closure or function is if you think it even possible for it to wrap a new instance of a variable.

You seem to be of the same misunderstanding as another user above that `let x = x;`-esque behavior would affect this issue in any way rather than be an irrelevant line.

> As python says in "import this": "Explicit is better than implicit."

Not only is this very rich coming from a language that uses exceptions for flow control, and the same syntax for initialization and assignment, it's also irrelevant and not a matter of explicitness versus implicitness.

Whether wrapping variables would be implicit or explicit in this context would not have solved the issue at all. Even if Python mandated explicit wrapping or made shadowing illegal, which some languages do, for which there is argument to be had, it would not change this subtle bug at all.

Re: Giving Ada a Chance

#216
post #202

Earlier quoted context omitted.

All you need is "x=x" to make a copy: list = [] for x in iterator: list.append(lambda y, x=x: some_code_that_closes_over(x)) That way the closure is explicit and much clearer when you look at it later. ("list" is a builtin, so not a good variable name.)

> All you need is "x=x" to make a copy: Your code is illegal Python on two levels. - Lambdas in Python do not allow assignment at all. - Variables in Python cannot be assigned using themselves in their r.h.s., without first being assigned something else, because Python's scoping is strange. You serve well as an example of a programmer that does not understand Python 's semantics here, because they are very counter-in…

Maybe you should try running it before jumping straight to insults? My code is perfectly legal in Python and runs exactly as I said.

Yours:

    In [1]: list = []
       ...: for x in range(5):
       ...:   list.append(lambda y: y+x)
       ...: [f(100) for f in list]
    Out[1]: [104, 104, 104, 104, 104]
Mine:

    In [2]: list = []
       ...: for x in range(5):
       ...:   list.append(lambda y, x=x: y+x)
       ...: [f(100) for f in list]
    Out[2]: [100, 101, 102, 103, 104]
I understand the semantics just fine, and it's very clear what's going on. In my version, each lambda has two parameters, one called y, and one called x. The one called x has a default value, and the default value is whatever the loop variable x's current value is: a copy is made of the loop variable's value, and stored as a default parameter value. There is no assignment, and a variable is not assigned to itself. The parameter has the same name, but the scope is different; within the lambda, the parameter shadows external variables, just as in any function.

This is idiomatic Python code, taught to beginners (search for n=n - it's about this same loop thing): https://realpython.com/python-lambda/

You do the same thing when passing parameters through to an inner function, like this:

  def outer(x, y):
    def inner(z, x=x):
      return z+x
    print(inner(2*y))
Shadowing list in a function scope is fine (just confusing), but your code does it in the global scope.

You seem to have a bone to pick with Python for some reason, but to me your attempts at criticism fall completely flat, as they are either factually incorrect or amount to "Python is different from [language x]".

Re: Giving Ada a Chance

#217
post #182

Earlier quoted context omitted.

Javascript is too weird to be a teaching language. Lexical scoping, invisible and sometimes unintuitive conversion rules, challenging runtime environments, and asynchronous code are important concepts but I wouldn't put them into an intro to programming class. IMO the ideal programming track would be as follows: Intro to programming track: * Assembly --> to understand the basics of how computers work and learn simple…

I really don't think you should use C as a teaching language. There are way too many pitfalls, and using libraries is quite cumbersome.

All of my academic computer science instruction was done in C. I was going to mention this in a response to one of the above comments regarding the pedagogical value of Python: I don't feel that taking onboard the concerns that come with programming in C (such as memory management) really made learning about computer science's fundamental algorithms and abstract data structures any easier. Many of the abstract data structures in question, such as trees and hash-maps, when practically implemented are dependent on an understanding of pointers that makes C an ideal pedagogical tool in at least one aspect.

I don't think it's really practical for every scenario. I still think it's a great tool for teaching students about many aspects of how a modern computer, or operating-system functions. You definitely won't get far in gaining a holistic understanding of any modern operating-system without understanding C.

Re: Giving Ada a Chance

#218
post #177

Earlier quoted context omitted.

> People found it frustrating how much work it'd take to get their programs to even compile Makes sense if they're students working on small projects. Ada is explicitly designed to make large programs readable, and willingly trades off on writeability when the two come into conflict. It isn't going to shine if you're writing small 'single shot' applications, that isn't what Ada is for. (Ada also commits to using many…

I really think a semantic facelift would do wonders for ADA, and shouldn't make much of a difference for readability. I know a lot of people are turned off by this.

I suspect overhauling the syntax is the kind of change that the committee would see as rather dramatic and with limited payoff. Introducing an optional alternative syntax alongside the existing one, would likely be seen as opening the door to syntactic inconsistencies, more likely to harm readability than to enhance it. The Python community has a relevant saying: There should be one -- and preferably only one -- obvious way to do it. [0]

I don't think it's a serious problem that Ada uses and then rather than &&, and uses or else rather than ||. It's the kind of thing you can get used to with time.

[0] https://www.python.org/dev/peps/pep-0020/

Re: Giving Ada a Chance

#219
post #119

I like the "clean feel" of Ada's syntax: it combines the elegance of Python with a bit more structure and does not suffer from Python's significant whitespace issues. The so-called "Ada comb" structure that is used for packages, subprograms, and even declare blocks makes it easy to find what you are looking for because it makes the source code more regular. The "Ada comb" is formed by the shape of the source code wit…

> The so-called "Ada comb" structure that is used for packages, subprograms, and even declare blocks makes it easy to find what you are looking for because it makes the source code more regular. "Declare" blocks are a PITA. I don't mean the declaration part in the subprogram example you quoted (that's fine), but having to use explicit "declare" blocks to create new variables in the scope of a loop or a conditional. Y…

I think everything you say is explained correctly, just that you describe a very special fringe case. The variables in your `declare` block only need to exit during the if, so this is one way to manage their lifespan.

In Ada as in almost every language you are well advised to have small functions with limited scope and then build on top of them. This would mean if you would declare your variables as normal part of a function the variables would have the lifespan of the function, and the function being small and to the point would also not be that long and you can avoid all the additional effort with `declare`.

I am not saying that there is no use for `declare` but it’s not needed that often or a big hassle in my view.

Ada is a bit more verbose than other languages and has one of the main aims to be readable on the basis that you write it once and read it many times.

Re: Giving Ada a Chance

#220
post #182

Earlier quoted context omitted.

I'd vote for Javascript over Python.

Javascript is too weird to be a teaching language. Lexical scoping, invisible and sometimes unintuitive conversion rules, challenging runtime environments, and asynchronous code are important concepts but I wouldn't put them into an intro to programming class. IMO the ideal programming track would be as follows: Intro to programming track: * Assembly --> to understand the basics of how computers work and learn simple…

> Java --> to learn interfaces, object oriented code, and an introduction to functional programming concepts like lambdas, pure functions, the value of immutable objects

Jesus Christ, Java as an introduction to functional programming? I don't even know what to say.

Post reply on HN