Tag Info

Hot answers tagged

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.


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 ...


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 ...


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 ...


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 ...


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 ...


15

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 ...


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 ...


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 ...


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 ...


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) { ...


7

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 ...


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 ...


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 ...


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 ...


4

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 ...


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 ...


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 ...


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 ...


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 ...


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 ...


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 ...


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 ...


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

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 ...


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. ...


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 ...


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

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 ...



Only top voted, non community-wiki answers of a minimum length are eligible