Tag Info

New answers tagged

2

My experience with large systems is that they stand or fall not by language choice, but by issues of design/architecture or test coverage. I'd rather have a talented Python team on my big enterprise project, than a mediocre Java one. Having said that, any language that let's you write significantly less code, has to be worth looking at (e.g. Python vs ...


12

Types form a monoid in two ways, together making a semiring. That's what's called algebraic data types. For finite types, this semiring directly relates to the semiring of natural numbers (including zero), which means you count how many possible values the type has (excluding “nonterminating values”). The bottom type (I'll call it Void) has ...


-3

If a function's return type is the bottom type, that means that it does not return. Period. In theory there is nothing to return, so for this purpose we can say it does not return, but of course it does return because the program must continue. Maybe it loops forever, or maybe it throws an exception. This would be the programmer's issue, not the ...


1

Yes this is a quite useful type; while its role would be mostly interior to the type system, there are some occasion where the bottom type would appear in openly. Consider a statically typed language in which conditionals are expressions (so the if-then-else construction doubles as the ternary operator of C and friends, and there might be a similar ...


3

It is useful for static analysis to document the fact that a particular code path is not reachable. For example if you write the following in C#: int F(int arg) { if (arg != 0) return arg + 1; //some computation else Assert(false); //this throws but the compiler does not know that } void Assert(bool cond) { if (!cond) throw ...; } The compiler will ...


18

Karl's answer is good. Here is an additional use that I don't think anyone else has mentioned. The type of if E then A else B should be a type that includes all the values in the type of A and all the values in the type of B. If the type of B is Nothing, then the type of the if expression can be the type of A. I'll often declare a routine def ...


3

You are by far not the first person to have this need; many others have had it before you, so there has been quite a bit of research on the subject, and a solution in almost every decent language out there. For a theoretical discussion, you might want to look at Programming by Contract, preconditions, postconditions and invariants. It includes a list of ...


24

I'll take a simple example: C++ vs Rust. Here is a function used to throw an exception in C++11: [[noreturn]] void ThrowException(char const* message, char const* file, int line, char const* function); And here is the equivalent in Rust: fn ...


1

Some languages (Including Delphi where I used it for this very purpose) have the concept of setting variables that can be checked at compile time so you can use an "IFDEF debug" directive to surround your scaffolding code and define / undefine debug appropriately. IIRC you would set the variables in a dialog at the program level. If your language is a ...


14

Maybe it loops forever, or maybe it throws an exception. Sounds like a useful type to have in those situations, rare though they may be. Also, even though Nothing (Scala's name for the bottom type) can have no values, List[Nothing] does not have that restriction, which makes it useful as the type of an empty list. Most languages get around this by ...


1

In some languages, null has the bottom type, since the subtype of all types nicely defines what languages use null for (despite the mild contradiction of having null be both itself and a function that returns itself, avoiding the common arguments about why bot should be uninhabited). It can also be used as a catch-all in function types (any -> bot) to ...


4

You might do well to take an assembly language class. It would clarify for you how data is actually represented in memory. Types are just an abstract construct to make working with data easier. You don't need them for programming. You just need to agree on a representation. For example, one of the simpler representations for a string is an array of ...


1

First, many compiled implementations of programming languages have builtins or intrinsics, that is functions which are known to the compiler and which are compiled in a special way. For C or C++ compiled by GCC there are many builtin functions. Ocaml has external functions, etc.... Then, some implementations offer some way to use the underlying ...


5

"I'm incapable of imagining things like how the code to convert a string to a number or a number to a string would be written." Aw, WTF, I may as well just post it. So below is simple C code that converts binary to decimal and back again. I wrote it long ago for a project in which the target was an embedded processor and the development tools had a ...


6

In general, you use higher-rank polymorphism when you want the callee to be able to select the value of a type parameter, rather than the caller. For example: f :: (forall a. Show a => a -> Int) -> (Int, Int) f g = (g "one", g 2) Any function g that I pass to this f must be able to give me an Int from a value of some type, where the only thing g ...


3

Higher rank polymorphism is extremely useful. In System F (the core language of typed FP languages you're familiar with), this is essential for admitting "typed Church encodings" which is actually how System F does programming. Without these, system F is completely useless. In System F, we define numbers as Nat = forall c. (c -> c) -> c -> c ...


1

Since no one else has answered the question, I think I'll give it a go myself. I'm going to have to get a bit philosophical. Generic programming is all about abstracting over similar types, without the loss of type information (which is what happens with object-oriented value polymorphism). In order to do this, the types must necessarily share some sort of ...


0

You mean like InterlockedExchange or like STL's swap, though neither are language specific STL is close enough.


4

In some languages, variables cannot be assigned (more than once, at their definition). In particular in functional languages like Ocaml (or Haskell) variables cannot be swapped (this would be meaningless). In these languages mutable references or mutable fields are not the same as variables. And functional languages don't have commands or statements, but ...


15

Because you usually don't need it Swapping the contents of two variables is a task that is mostly used at university or programming classes. It's real-life use is limited to sorting algorithms and (maybe?) encryption. If you are writing a algorithm like this, you are probably capable of swapping two values anyway. That being said, most programming ...


3

There are other methods for doing the same thing. Some of which are more flexible, allowing rotation. In Python: x = 1 y = 2 z = 3 (x, y) = (y, x) print x, y (x, y, z) = (z, x, y) print x, y, z Other languages allow similar behavior.


3

But when I did math at school "let x = 123 " was common phrasing. Early versions of Basic insisted on the "let" keyword before the equal. So its basically boils done to "let" is understood. A key driver not usually considered but very important at the time it what did you actually type on. There were two feasible input devices, The "teletype" whereby ...


8

According to Wikipedia, the use of equals for assignment dates back to Heinz Rutishauser's language Superplan, designed from 1949 to 1951, and was particularly popularized by Fortran: A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957, and has blindly been copied by armies of language ...


2

It's because the most crucial thing in academic education is to teach you to use the correct terminology to describe the things you do. List is something other that array. and You can't use java.util.List in Java because it's an interface. You usually use java.util.ArrayList which being a List implementation, is not a list, but an object wrapper around ...


1

You've literally never seen or used any arrays at all? We use them all the time, in addition to lists. We usually don't use Java, but we do use plenty of other languages that draw obvious similarities. Between arrays and Lists, one's lighter-weight and, additionally, more to-the-point, whereas the other's got more functionality. As a general rule in ...


1

The distinction is a matter of degree. Consider the security procedures at a major bank. Every morning the first employe to appear moves a certain potted plant to a subtly different location to signal all other employees "It's okay, there is NOT a criminal gang lurking in the back office trying to kidnap the first two employees to show up and to get them to ...


13

One reason that first-year programming classes use arrays is legacy: that's how the professors originally learned it before we started using standard libraries with dynamic lists baked in. Using the primitive data types is also more generally applicable: arrays exist in pretty much any computer language under the sun (and can be implemented in a handful of ...


1

Are there any good use-cases for variable variables? It depends on your definition of "good". My personal answer is simply no. I tried to come up with one just to answer this question, I raked help questions on SO where people are using them, and read a guides and blog posts. I just cannot find a scenario where it can be argued using a variable ...


7

Java allows variables of any type to be stored into arrays. By contrast, ArrayList only allows storage of references. One may thus not usefully discuss ArrayList without first covering how auto-boxing will convert primitives to reference types, and how auto-unboxing will sometimes convert reference types to primitives: for (int i=10; i<=10000; i*=10) { ...


2

Assuming that list are indeed easier to work with, as you say — that doesn't really matter. Learning is more about "basic to complex" than "easy to hard". If fundamentals weren't important, then computer science wouldn't be an academic field. You could just learn how to click together apps using existing frameworks/libraries from online tutorials. ...


6

I think it makes sense to teach how to use arrays first due to the fact that ArrayList uses an array internally. The ArrayList class has a member variable called elementData which is an Object array. From the JDK ArrayList source code: /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the ...


18

The answers above are great, but I have another in mind. Java's main() method means that students encounter basic arrays very early on, often as soon as the first day of class. Why? public static void main(String[] args) It's the first thing you're going to have to deal with to write Hello World and beyond. (I've seen some courses use teaching IDEs like ...


45

They probably wanted to start with the data structure that most accurately represents how a computer works, so you're familiar with the fundamentals before they start introducing higher-level abstractions like Lists which make it easier to work with. Otherwise you'd have no way of understanding why a certain structure or algorithm is slow/fast for some ...


112

Because arrays teach concepts like indexing and bounds, two fundamentally important concepts in computer programming. Lists are not a "standard." Contrary to popular belief, there are still a wide variety of problem spaces for which arrays are a perfect fit.


2

An HTML document consists of strings of text surrounded by pairs of markup tags, so...there isn't really any shortage of places to put a string. You could have an "HTML document" that's nothing but the string. You could have the usual head/body and a single div in the body whose content is the string. One of the divs could contain Lua source code for all we ...


1

ECMAScript ist the single most compatibility-constrained language that ever existed. In general, developers can't choose the version of the implementation (or even which implementation) that is going to be used to run their code, because it is installed on the users' machine. Users can't choose the version of the app they run, because the code gets ...


2

I disagree, the main goal of ES6 is not to introduced new ways "to accomplish the same thing" but actually cover some areas that were not covered at all. Generators are extremely helpful for the class of tasks where you should iterate through a big list of items but you don't need to keep all this items in memory at one time. This is something that just was ...


2

Technically, a formal language (programming languages are a subset of those) is a set of strings that are valid in that language. For a language to be useful, it also has to have semantics, i.e. what does a string in the language mean. For example, print('hello') is a valid string in the language Python and { "name": "zukerman" } is a valid string in the ...


0

The point is that the same Instruction Set can be used differently. And in practice, for efficiency purposes, that may matter a lot. An optimizing compiler can (and will, e.g. GCC with -O2 -mtune=native) produce different machine code for the same ISA, depending on the target processor. So the code optimized for an Intel processor is not the same as the ...


1

Note that Tanenbaum talks about what is exposed to low level programmers, not what happens at the level of CPU execution cycles. Down at that level are various micro operations that are involved in branch prediction, prefetch, pipelining, etc. Machine code instructions get broken down into smaller chunks after the point where programmers have direct access. ...


5

It looks like you borrowed some vocabulary from theoretical computer science (regarding the words "set" and "language"), and tried to use that to interpret the textbook description of the lower level computer systems (CPU and hardware). The word "set" as in "instruction set architecture" refers to the set of predefined opcodes that are valid for the given ...


2

Is there still any functional programming language that doesn't rely on C runtime? The answer to this exact question is yes: C is not the only low-level language which is suitable as a compilation target (although definitely the most popular one). For example, the Embedded ML described in Functional Programming for Embedded Flight Software ...


1

In principle? Yes. C is a programming language like all others. What C can do, other languages can be made to do. In practice? Rarely. C is near-ubiquitous as a systems programming language (there are exceptions, but they will be provided in the outraged comments, so I don't bother listing them), and it is almost always a better idea to use what is already ...


-2

You could/should read up on the following topics for preparation: finite state machines formal and context-free grammar parse tree Extended Backus–Naur Form For a start you could examine the scheme language. It is pretty easy to parse and analyse. You could develop something similar.



Top 50 recent answers are included