Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== H02. Tree Sets ====== ===== Problem statement ===== ===== Implementation details ===== About ''andThen'' ===== Implementation ===== Write a function which splits a text using the single whitespace character as a separator. Multiple whitespaces should be treated as a single separator. If the list contains only whitespaces, ''split'' should return the empty list. <code scala> /* split(List('h','i',' ','t','h','e','r','e')) = List(List('h','i'), List('t','h','e','r','e')) */ def split(text: List[Char]): List[List[Char]] = ??? </code> Write a function which computes a list of ''Token'' from a list of strings. Recall that Tokens keep track of the string frequency. Use an auxiliary function ''insWord'' which inserts a new string in a list of Tokens. If the string is already a token, its frequency is incremented, otherwise it is added as a new token. <code scala> def computeTokens(words: List[String]): List[Token] = { /* insert a new string in a list of tokens */ def insWord(s: String, acc: List[Token]): List[Token] = ??? def aux(rest: List[String], acc: List[Token]): List[Token] = ??? ??? } </code> Write a function ''tokensToTree'' which creates a ''WTree'' from a list of tokens. Use the insertion function ''ins'' which is already implemented. (//Hint: you can implement it as a single fold call, but you have to choose the right one//) <code scala> def tokensToTree(tokens: List[Token]): WTree = ?? </code> Write a function ''makeTree'' which takes a string and builds a ''WTree''. ''makeTree'' relies on all the previous functions you implemented. You should use ''_.toList'', which converts a ''String'' to ''List[Char]''. You can also use ''andThen'', which allows writing a concise and clear implementation. <code scala> def makeTree(s:String): WTree = ??? </code> Implement a ''filter'' method in the In the code template you will find a string: ''scalaDescription''. Compute the number of occurrences of the keyword "Scala" in ''scalaDescription'': <code scala> def scalaFreq: Int = ??? </code> ===== Using andThen ===== Suppose we have two functions ''f: B => C'' and ''g: A => B''. The functional composition $math[f \circ g], can be defined in Scala as: ''x => f(g(x))''. The intuition behind composition is that we first apply ''g'' and then ''f'', on the formal parameter ''x''. In other words, we apply ''g'' **and then** ''f''. The (higher-order) function ''andThen'' in Scala works in precisely the same way: ''g.andThen(f)'' represents the function ''x => f(g(x))''. Here is an example: <code scala> ((x: Int) => x * 2).andThen((x:Int) => x + 1) // the function g.andThen(f) where g(x) = 2*x and f(x) = x + 1 ((x: Int) => x * 2).andThen((x:Int) => x + 1)(2) // calling the previous function with parameter 2 </code> ''andThen'' is especially useful when we want to sequence several functions. Thus, instead of: <code scala> function1(function2(function3(x))) </code> which may become less intuitive as the number of applied function grows, we may use: <code scala> function1 .andThen(function2) .andThen(function3)(x) </code> ===== Submission rules ===== ==== Project format ==== * **You should not change any other files of the project, except for the //template-file//**. For this homework, the //template-file// is ''FSets.scala''. **Warning:** if a submission has changes in other files, it **may not be graded**. * To solve your homework, download the {{:fp:h1-fsets.zip| Homework project}} and **rename** it using the following convention: ''HX_<LastName>_<FirstName>'', where X is the homework number. (Example: ''H1_Popovici_Matei''). If your project name disregards this convention, it **may not be graded**. * Each project file contains a ''profileID'' definition which you must fill out with your token ID received via email for this lecture. Make sure the token id is defined correctly. (Grades will be automatically assigned by token ID). * In order to be graded, **the homework must compile**. If a homework has **compilation errors** (does not compile), it **will not be graded**. Please take care to remove code that does not compile by replacing (or keeping) function bodies implemented with ''???''. ==== Submission ==== * Your submission should be an **archived file** with your solved project, named via the convention specified previously. * **All homework must be submitted via moodle. Submissions sent via email will not be graded!**. * **All homework must be submitted before the deadline**. Submissions that miss the deadline (even by minutes) will not be graded. (All deadlines will be fixed at 8:00 AM, so that you can take advantage of an all-nighter, should you choose to). ==== Points ==== * Points are assigned for each test (for a total of 100p), but the final grade will be assigned **after manual review**. Selectively, a homework **may be required to be presented** during lab for the final grade. ==== Integrity ==== * Each homework uses public test-cases which you can use to guide and test your implementation. Most test-cases are simple, in order to be as easy to use as possible. **If an implementation is written with the sole purpose of passing those specific tests, thus disregarding the statement, the entire homework will not be graded!** * The homework must be solved **individually** - you are not allowed to share or to take code from other sources including the Internet. We strongly encourage you to **ask questions** via the forum (instead of MS Teams) so that other students can benefit from the answers and discussion. You may ask questions about the homework during lab. You will receive feedback about your implementation ideas, but not on the actual written code.