0% found this document useful (0 votes)
153 views2 pages

Tut 6

The document provides sample questions and problems related to functional programming in Scala. It asks the reader to write functions using recursive and high-order function approaches to: 1) Return a list of squares of numbers from 1 to n. 2) Calculate x to the power of n without using math library functions. 3) Append and reverse lists without using built-in functions. 4) Return a sublist of elements in a list that are less than a given number. 5) Look up an element in a heterogeneous list based on a string component.

Uploaded by

Lê Duy Bình
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views2 pages

Tut 6

The document provides sample questions and problems related to functional programming in Scala. It asks the reader to write functions using recursive and high-order function approaches to: 1) Return a list of squares of numbers from 1 to n. 2) Calculate x to the power of n without using math library functions. 3) Append and reverse lists without using built-in functions. 4) Return a sublist of elements in a list that are less than a given number. 5) Look up an element in a heterogeneous list based on a string component.

Uploaded by

Lê Duy Bình
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Principles of Programming Languages

Tutorial
Functional Programming

Question 1.
Write a function lstSquare(n:Int) that returns a list of the squares of the numbers from
1 to n?
For example:
lstSquare(5) returns List(1,4,9,16,25)

a) Use recursive approach?

b) Use high-order function approach?

Question 2.
Write a Scala function pow(x: Double, n: Int): Double that return a power xn . (dont use
Scala library math).
For example:
pow(2,0) returns 1
pow(2,3) returns 8

a. Use recursive approach

b. Use high-order function approach

Question 3.
Make a list of the elements 1, 2, 3, 5, 7.
Write your own functions (dont use Scala library functions: append, reverse):
append(a: List[Int], b: List[Int]): List[Int]
reverse(a: List[Int]): List[Int]

a. Use recursive approach

b. Use high-order function approach

Question 4.
Write a Scala function lessThan(n: Int, lst: List[Int]) that returns a list of all numbers in
lst less than n. For example, lessThan(50, List(1, 55, 6, 2)) yields List(1,6,2)

a. Use recursive approach

b. Use high-order function approach

1
Principles of Programming Languages

Question 5.
Assume that there are many different data structures which contain a component in String
type. Write your own function lookup that can find in a list an element whose component
is the specified string.
Hint: The prototype of the function should be:
lookup[T](str:String,lst:List[T],ret: T => String):Option[T]
For example, assume that there are the following classes:
case class A(n:String,v:Int)
case class B(x:Int,y:A)
lookup("m",List(A("n",3),A("m",5)),(x:A)=>x.n) returns Some(A("m",5))

You might also like