silikontalking.blogg.se

Scala tutorial
Scala tutorial








Now, let’s introduce another type, Double, for working with real-valued numbers. If we wanted to override that, we’d need to use parentheses to indicate it, just like in basic algebra. Notice that Scala knows that multiplication takes precedence over addition in such computations. scala> x + 3Īnd of course, we can assign the results of such computations to other variables. Returning to the variable x, we can now use it for other computations. Typical names for variables will be strings like x, y1, result, firstName, here_is_a_long_variable_name. They must not be identical to one of the “reserved words” that Scala has already defined, such as for, if, def, val, and var.Variable names may contain: letters, numbers, underscore.

scala tutorial

You can choose the names for variables, but they must follow some rules. Here, x is a variable, which we’ve indicate by prefacing it with val, which indicates it is a fixed variable whose value cannot change. Let’s do this trivially to start with, breaking down the print statement above into two steps. We often need to store the result of evaluating an expression to a variable for later use (in fact, programming doesn’t get done with out doing this). You can think of the print command as a verb, and its parameter (e.g.

  • Scala passes that value to the command print.
  • Note that the result is the action of printing, not a value with a type. We can ask Scala to display the result of evaluating a given expression by using the print command. So, if you consider the String “2? and the String “3?, and use the + operator on them, you don’t get “5? - instead you get “23?. With Strings, addition doesn’t make sense, but the + operator instead indicates concatenation of the strings. No surprise, the result is what you think it should be. Scala evaluates the result and prints the result to the screen. For example, Ints can be added to each other. Scala knows that different actions are afforded by different types. This allows Scala to know what to do when you want to use the numerical value 2 (an Int) or the character representing 2 (a String). Strings and Ints are types - this is an easy but important distinction that sometimes takes beginning programmers a while to get used to. The REPL tells us that the first is a String which contains the characters Hello world, that the second is an Int whose value is the integer 2. The scala> line is the prompt that the REPL is waiting for you to enter expressions. Type in expressions to have them evaluated. You should see something like the following: Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). In particular, programming languages are very exact, so they’ll do exactly what you tell them to do - and you’ll almost surely mess a few things up, and learn from that. If you just read them over, it will in many cases look quite obvious (and it is), but someone who is new to programming will generally find many gaps in their understanding by actually trying things out. Note: it is very important that you actually type the commands given below into the REPL. REPL stands for read-eval(uate)-print-loop, which means it is a program that (1) reads the expressions you type in, (2) evaluates them using the Scala compiler, (3) prints out the result of the evaluation, and then (4) waits for you to enter further expressions. We’ll use the Scala REPL for entering Scala expressions and seeing what the result of evaluating them is. A (partial) starter tour of Scala expressions, variables, and basic types

    scala tutorial scala tutorial

    #SCALA TUTORIAL CODE#

    If you are having problems with this, you might try the examples by evaluating them in the code box on SimplyScala. This tutorial assumes you have Scala installed and that you are using some form of Unix (if you use Windows, you’ll want to look into Cygwin). (Though perhaps some of the later ones on functional programming and such that I intend to do will be, so check back.) In the meantime, check out existing Scala learning materials I’ve listed in the links page for the course. Note: if you already know a programming language, this tutorial will probably not be very useful for you. The one exception I’m aware of is SimplyScala. These tutorials assume no previous programming background, an assumption which is unfortunately still quite rare in the help-folks-learn-Scala universe, and which more or less necessitates the creation of these tutorials. The is the first of several Scala tutorials I’m creating for my Fall 2011 graduate Introduction to Computational Linguistics course at UT Austin, loosely based on similar tutorials that Katrin Erk created for teaching Python in a similar course.








    Scala tutorial