![]() ![]() We need a new language for logical expressions, The interface is defined in the file tell-ask.lisp. Knowledge bases, and tell and ask operations on The logic system covers part III of the book. I hope this Scala List class tutorial has been helpful.Logic (Subsystem of AIMA Code) Logic (Subsystem of AIMA Code) In summary, as you have seen, you can create Scala lists in several different ways, including these approaches: Scala> val x = List.tabulate(5)(n => n * n)Īs you can see, that example creates a List of five elements, where the element values are the square of the index of each element, so 0 becomes 0, 1 becomes 1, 2 becomes 4, 3 becomes 9, and 4 becomes 16. The book Programming in Scala shows how to create a List using a simple "squares" function with the tabulate method: The tabulate method creates a new List whose elements are created according to the function you supply. Create a Scala List with the List class ‘tabulate’ methodįinally, you can create a Scala List with the tabulate method of the List class. X: List = List(foo, foo, foo)Īs you can see, you just specify how many items you want, and the object value you want to fill each List element with. In addition to this simple approach, the range function can also take a third argument, which serves as a "step" value when creating the List:Ĭreate a Scala List with the List class ‘fill’ methodĪnother convenient way to create a Scala List is with the fill method: X: List = List(1, 2, 3, 4, 5, 6, 7, 8, 9)Īs you can see, this example creates a List of Int values, beginning at 1, and ending at 9. Create a Scala List with the ‘range’ methodĪnother convenient way to create a List is with the range method: In this example I've explicitly stated that the values in the List are of the Number type. This example demonstrates the syntax to specify the List type: ![]() Note that if you're going to mix types in a List constructor, you may need to manually specify the type of the List. ![]() This syntax looks a lot like the Java way to create an object, except (a) you don't need the "new" keyword before the List, and (b) you don't have to declare the type of elements in the List. My guess is that the most popular way to create a List is with what I call the "Java style": But I first learned about the :: approach many years ago with Lisp. (And yes, :: is a function/method.)Īrguably using this style can also be thought of as a Haskell or functional programming (FP) style. In this “cons” style, the :: method takes two arguments, a “head,” which is a single element, and a “tail,” which is a List. With this approach, you need to end the list with the Nil object. Create a Scala List in the Lisp styleįirst, if you prefer the Lisp-style of programming, you can create a Scala List using the “cons” syntax, like this:Īs you can see, this creates a List that contains the integers 1, 2, and 3. I'll execute each command in the Scala command-line interpreter so you can see the results of each approach. In this Scala List tutorial, I'll demonstrate each of these approaches. You can create a Scala List in several different ways, including these approaches: Scala List class FAQ: How do I create a List in Scala?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |