do this in scala LAB 5 Remark: If the exercise gives the name of a function and the…
- User
do this in scala LAB 5 Remark: If the exercise gives the name of a function and the arguments it takes, then the function must look like this. You can create a new function inside this one that takes more arguments! Recall the principles of functional programming that you learned in previous classes, including: pure functions and immutability. Wherever you work with lists, use pattern matching instead of plain if-else whenever possible. Whenever possible, use tail recursion instead of standard recursion. Prepare some examples showing how your code works - remember to include boundary values, e.g. empty lists, all elements that meet/fail conditions, different functions in high-order functions, etc. All of the above will be taken into account when grading assignments. Exercise 1 2p Create an ADT to represent a traffic light with three possible states: Red, Yellow, and Green. Implement a function that takes a TrafficLight instance and returns an action as a String based on the light’s color. • Define a sealed trait called TrafficLight. • Define case objects for each light color (Red, Yellow, Green) extending TrafficLight. Write a function called trafficAction that takes a TrafficLight and returns a String with the following rules: • "Stop" if the light is Red • "Slow down" if the light is Yellow • "Go" if the light is Green Exercise 2 2p Create an ADT to represent a binary tree of integers and then implement a recursive function to calculate the sum of all values in the tree. Exercise 3 2p Implement additional functions for your binary tree: • maxDepth – a function that returns the maximum depth of the tree (the root depth is 1). • isBalanced – a function that checks if the tree is balanced. A binary tree is balanced if for each node the difference in the depth of the left and right subtrees is not more than 1. • sumTreeWithCondition - a function that accepts an additional parameter: a function condition of type Int => Boolean. • transformTree - a function that applies a transformation to each value in the tree. Exercise 4 4p Use algebraic data types to define a simple linked list. The list should consist of an empty list, represented by the case object Nil and a non-empty list node. Implement methods: • length - that calculates the length of the linked list • map - method that takes a function f as an argument and returns a new list with f applied to each element. • reverse - method that reverses the linked list. • filter - method that takes a predicate function (A => Boolean) and returns a new linked list containing only the elements that satisfy the predicate. • toList - method toList that converts a LinkedList to a standard Scala List • contains - a method that checks if a linked list contains a specific element. The method should return true if the element is found, and false otherwise. • zipWith - method that takes two linked lists and a function f and combines the lists by applying f to each pair of corresponding elements. If the lists have different lengths, only combine elements up to the length of the shorter list.
- Assistant