Summary

This document contains practice questions related to functional programming concepts, likely written in Scala, with code examples and functions like 'abs', 'dots', and 'shorten'.

Full Transcript

def abs(x: Int): Int = if x > 0 then x else -x def dots(length: Int) = "." * length dots(abs(-3)) //abs(-3); dots(-3) (dots compose abs)(-3) (abs andThen dots)(-3) object Graph: class Edge(from: Node, to: Node) class Node: infix def to(that: Node): Edge = Edge(this, that) def...

def abs(x: Int): Int = if x > 0 then x else -x def dots(length: Int) = "." * length dots(abs(-3)) //abs(-3); dots(-3) (dots compose abs)(-3) (abs andThen dots)(-3) object Graph: class Edge(from: Node, to: Node) class Node: infix def to(that: Node): Edge = Edge(this, that) def --> (that: Node): Edge = Edge(this, that) import Graph.* val a = Node() val b = Node() a.to(b) a --> b // a.-->(b) a to b def shorten(str: String, maxLen: Int): String = if str.length > maxLen then str.substring(0, maxLen - 3) + "..." else str shorten("Functional Programming", 20) extension (str: String) def short(maxLen: Int): String = shorten(str, maxLen) "Functional Programming".short(20) def abs1(x: Int): Int = def max(a: Int, b: Int) = if a > b then a else b max(x, -x) abs1(-3) def abs2(x: Int): Int = def maxX(a: Int) = if a > x then a else x maxX(-x) abs2(-3)

Use Quizgecko on...
Browser
Browser