Difference between these two method definitions - function

What's the difference between these two definitions?:
def sayTwords(word1: String, word2: String) = println(word1 + " " + word2)
def sayTwords2(word1: String)(word2: String) = println(word1 + " " + word2)
What is the purpose of each?

The second is curried, the first isn't. For a discussion of why you might choose to curry a method, see What's the rationale behind curried functions in Scala?

sayTwords2 allows the method to be partially applied.
val sayHelloAnd = sayTwords2("Hello")
sayHelloAnd("World!")
sayHaelloAnd("Universe!")
Note you can also use the first function in the same way.
val sayHelloAnd = sayTwords("Hello", _:String)
sayHelloAnd("World!")
sayHelloAnd("Universe!")

def sayTwords(word1: String, word2: String) = println(word1 + " " + word2)
def sayTwords2(word1: String)(word2: String) = println(word1 + " " + word2)
The first contains a single parameter list. The second contains multiple parameter lists.
They differ in following regards:
Partial application syntax. Observe:
scala> val f = sayTwords("hello", _: String)
f: String => Unit = <function1>
scala> f("world")
hello world
scala> val g = sayTwords2("hello") _
g: String => Unit = <function1>
scala> g("world")
hello world
The former has a benefit of being positional syntax. Thus you can partially apply arguments in any positions.
Type inference. The type inference in Scala works per parameter list, and goes from left to right. So given a case, one might facilitate better type inference than other. Observe:
scala> def unfold[A, B](seed: B, f: B => Option[(A, B)]): Seq[A] = {
| val s = Seq.newBuilder[A]
| var x = seed
| breakable {
| while (true) {
| f(x) match {
| case None => break
| case Some((r, x0)) => s += r; x = x0
| }
| }
| }
| s.result
| }
unfold: [A, B](seed: B, f: B => Option[(A, B)])Seq[A]
scala> unfold(11, x => if (x == 0) None else Some((x, x - 1)))
<console>:18: error: missing parameter type
unfold(11, x => if (x == 0) None else Some((x, x - 1)))
^
scala> unfold(11, (x: Int) => if (x == 0) None else Some((x, x - 1)))
res7: Seq[Int] = List(11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
scala> def unfold[A, B](seed: B)(f: B => Option[(A, B)]): Seq[A] = {
| val s = Seq.newBuilder[A]
| var x = seed
| breakable {
| while (true) {
| f(x) match {
| case None => break
| case Some((r, x0)) => s += r; x = x0
| }
| }
| }
| s.result
| }
unfold: [A, B](seed: B)(f: B => Option[(A, B)])Seq[A]
scala> unfold(11)(x => if (x == 0) None else Some((x, x - 1)))
res8: Seq[Int] = List(11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

Related

SML - Can't find where "uncaught exception Empty" is thrown in quicksort algorithm

I am trying to write a quicksort algorithm without using any List.nth functions. I've come up with this but when I try to test it, it ends up throwing an "uncaught exception Empty". I can't seem to find where this exception is thrown.
Here is my code:
(*returns last element in list*)
fun last (h::nil) = h
| last (h::lst) = last(lst)
| last _ = ~1;
(*returns middle element in list*)
fun middle (lst) =
let
fun middle_rec (_ :: []) (x :: _) = x
| middle_rec (_ :: _ :: []) (x :: _) = x
| middle_rec (_ :: _ :: xs) (_ :: ys) = middle_rec xs ys
| middle_rec _ _ = ~1
in
middle_rec lst lst
end;
(*return median of three elements*)
fun median(a,b,c) =
if ((b>a andalso a>c) orelse (c>a andalso a>b)) then a
else if ((a>b andalso b>c) orelse (c>b andalso b>a)) then b
else if ((a>c andalso c>b) orelse (b>c andalso c>a)) then c
else ~1;
(*partitions a list with one containing elements smaller than or equal to p and one with elements greater than p*)
fun partition([], p) = ([], [])
| partition(lst, p) =
let
fun part_rec ([], x::xs, y::ys, p) = (x::xs, y::ys)
| part_rec (lst, x, y, p) =
if hd(lst) <= p then part_rec(tl(lst), hd(lst)::x, y, p)
else part_rec(tl(lst), x, hd(lst)::y, p)
in
part_rec(lst,[],[],p)
end;
(*quicksort function*)
fun quicksort [] = []
| quicksort(x::xs) =
let
val (left, right) = partition(x::xs, median(x, middle(x::xs), last(x::xs)))
in
quicksort left # [x] # quicksort right
end;
quicksort([9, 4, 7, 2, 8, 5, 1, 6, 4, 3]);
Let's step through a very simple data set:
quicksort([1]);
val (left, right) = partition([1], median(1, middle([1]), last([1])))
val (left, right) = partition([1], median(1, 1, 1))
val (left, right) = partition([1], ~1)
val (left, right) = part_rec([1], [], [], ~1)
val (left, right) = part_rec([], [], [1], ~1)
And here we have a problem.
fun part_rec ([], x::xs, y::ys, p) = (x::xs, y::ys)
| part_rec (lst, x, y, p) =
if hd(lst) <= p then part_rec(tl(lst), hd(lst)::x, y, p)
else part_rec(tl(lst), x, hd(lst)::y, p)
The first pattern cannot apply, because the second argument is an empty list and thus cannot match with x::xs, so the second pattern must be applied. However, that means calling hd on an empty list, which will raise the Empty exception.

How to evaluate datatype and true_of_all_constants functions?

I am a beginner of sml and I'm taking programming language courses at Coursera. There's a datatype and function that I don't know how to evaluate:
datatype exp = constant of int
| Negate of exp
|Add of exp * exp
|Multiply of exp * exp
fun true_of_all_constants(f,e) =
case e of
constant i => f i
| Negate e1 => true_of_all_constants(f,e1)
| Add(e1,e2) => true_of_all_constants(f,e1)
andalso true_of_all_constants(f,e2)
| Multiply(e1,e2) => true_of_all_constants(f,e1)
andalso true_of_all_constants(f,e2)
Trying to evaluate them, I always get errors:
true_of_all_constants [3,4,5];
true_of_all_constants 4;
true_of_all_constants (is_even 4, 5);
where is_even is a little helper function:
fun is_even v =
(v mod 2 = 0)
To test true_of_all_constants, what should e be replaced with? Also, could you explain what does datatype do here? I don't understand why we need "Negate" or "Add" here; why we have "exp*exp," rather than "exp+exp," for "Add?"
Fixing whitespace, the data type definition and the definition of true_of_all_constants (p, e) become:
datatype exp =
Constant of int
| Negate of exp
| Add of exp * exp
| Multiply of exp * exp
fun true_of_all_constants (p, e) =
let fun aux (Constant i) = p i
| aux (Negate e1) = aux e1
| aux (Add (e1, e2)) = aux e1 andalso aux e2
| aux (Multiply (e1, e2)) = aux e1 andalso aux e2
in aux e end
Here constant has been renamed to Constant: Both will work, but naming constructors with an uppercase letter differentiates it visually from other identifiers. And I've used an inner function, aux, to shorten the recursive expressions a bit. Instead of f I called it p for predicate, but that's a taste thing.
Trying to evaluate them, I always get errors
Here are some examples of expressions and evaluating them:
- val two_plus_two = Add (Constant 2, Constant 2);
- true_of_all_constants (fn i => i = 2, two_plus_two);
> val it = true : bool
- val two_times_neg_two = Multiply (Constant 2, Constant ~2);
- true_of_all_constants (fn i => i > 0, two_times_neg_two);
> val it = false : bool
- val two_four_six = Add (Constant 2, Add (Constant 4, Constant 6));
- fun is_even x = x mod 2 = 0;
- true_of_all_constants (is_even, two_four_six);
> val it = true : bool
could you explain what does datatype do here?
I think you should refer to a book or tutorial here.
For example, ML for the Working Programmer, ch. 4 (free PDF) deals with datatype definitions.
I don't understand why we need "Negate" or "Add" here
I don't know, either. The problem you're given in the course is entirely hypothetical.

Extract Options from potentially null JSON values using for expression

I have a JSON document where some values can be null. Using for expressions in json4s, how I can yield None, instead of nothing?
The following will fail to yield when the value for either of the fields FormattedID or PlanEstimate is null.
val j: json4s.JValue = ...
for {
JObject(list) <- j
JField("FormattedID", JString(id)) <- list
JField("PlanEstimate", JDouble(points)) <- list
} yield (id, points)
For example:
import org.json4s._
import org.json4s.jackson.JsonMethods._
scala> parse("""{
| "FormattedID" : "the id",
| "PlanEstimate" : null
| }""")
res1: org.json4s.JValue = JObject(List((FormattedID,JString(the id)),
(PlanEstimate,JNull)))
scala> for {
| JObject(thing) <- res1
| JField("FormattedID", JString(id)) <- thing
| } yield id
res2: List[String] = List(the id)
scala> for {
| JObject(thing) <- res1
| JField("PlanEstimate", JDouble(points)) <- thing
| } yield points
res3: List[Double] = List()
// Ideally res3 should be List[Option[Double]] = List(None)
scala> object OptExtractors {
|
| // Define a custom extractor for using in for-comprehension.
| // It returns Some[Option[Double]], instead of Option[Double].
| object JDoubleOpt {
| def unapply(e: Any) = e match {
| case d: JDouble => Some(JDouble.unapply(d))
| case _ => Some(None)
| }
| }
| }
defined object OptExtractors
scala>
scala> val j = parse("""{
| "FormattedID" : "the id",
| "PlanEstimate" : null
| }""")
j: org.json4s.JValue = JObject(List((FormattedID,JString(the id)), (PlanEstimate,JNull)))
scala>
scala> import OptExtractors._
import OptExtractors._
scala>
scala> for {
| JObject(list) <- j
| JField("FormattedID", JString(id)) <- list
| JField("PlanEstimate", JDoubleOpt(points)) <- list
| } yield (id, points)
res1: List[(String, Option[Double])] = List((the id,None))
According to the documentation,
Any value can be optional. Field and value is completely removed when
it doesn't have a value.
scala> val json = ("name" -> "joe") ~ ("age" -> (None: Option[Int]))
scala> compact(render(json))
res4: String = {"name":"joe"}
Explaining why your for comprehension doesn't yield anything.
Of course, a null value is mapped to None internally.
The last command should look like:
for {
JObject(thing) <- res1
} yield thing.collectFirst{case JField("PlanEstimate", JDouble(points)) => points}
Or like
for {
JObject(thing) <- res1
points = thing.collectFirst{case JField("PlanEstimate", JDouble(p)) => p}
} yield points
What about this
for {
JObject(thing) <- res1
x = thing.find(_._1 == "PlanEstimate").flatMap(_._2.toOption.map(_.values))
} yield x

Scala Functional Literals with Implicits

Forgive me if this has already been asked elsewhere. I have a Scala syntax question involving function-values and implicit parameters.
I'm comfortable using implicits with Scala's currying feature. For instance if I had a sum function and wanted to make the second argument an implicit:
scala> def sum(a: Int)(implicit b: Int) = a + b
sum: (a: Int)(implicit b: Int)Int
Is there a way to do this using the function-value syntax? Ignoring the implicit for a moment, I typically write curried function-values like this:
scala> val sum2 = (a: Int) => (b: Int) => a + b
sum: (Int) => (Int) => Int = <function1>
However, the function signature in the second approach is much different (the currying is being expressed explicitly). Just adding the implicit keyword to b doesn't make much sense and the compiler complains as well:
scala> val sum2 = (a: Int) => (implicit b: Int) => a + b
<console>:1: error: '=>' expected but ')' found.
val sum2 = (a: Int) => (implicit b: Int) => a + b
^
Furthermore partially-applying sum from the very first approach to get a function-value causes problems as well:
scala> val sumFunction = sum _
<console>:14: error: could not find implicit value for parameter b: Int
val sumFunction = sum _
^
This leads me to believe that functions that have implicit parameters must have said parameters determined when the function-value is created, not when the function-value is applied later on. Is this really the case? Can you ever use an implicit parameter with a function-value?
Thanks for the help!
scala> val sum2 = (a: Int) => {implicit b: Int => a + b}
sum2: (Int) => (Int) => Int = <function1>
This will just make b an implicit value for the scope of the function body, so you can call methods that expect an implicit Int.
I don't think you can have implicit arguments for functions since then it is unclear what the function is. Is it Int => Int or () => Int?
The closest I found is:
scala> case class Foo(implicit b: Int) extends (Int => Int) {def apply(a: Int) = a + b}
defined class Foo
scala> implicit val b = 3
b: Int = 3
scala> Foo()
res22: Foo = <function1>
scala> res22(2)
res23: Int = 5
In this snippet
scala> val sum2 = (a: Int) => (b: Int) => a + b
sum: (Int) => (Int) => Int = <function1>
Note that the precise type of sum2 is Function1[Int, Function1[Int, Int]]. It could also be written as
val sum2 = new Function1[Int, Function1[Int, Int]] {
def apply(a: Int) = new Function1[Int, Int] {
def apply(b: Int) = a + b
}
}
Now, if you try to make b implicit, you get this:
scala> val sum2 = new Function1[Int, Function1[Int, Int]] {
| def apply(a: Int) = new Function1[Int, Int] {
| def apply(implicit b: Int) = a + b
| }
| }
<console>:8: error: object creation impossible, since method apply in trait Function1 of type (v1: Int)Int is not defined
def apply(a: Int) = new Function1[Int, Int] {
^
Or, in other words, Function's interfaces do not have implicit parameters, so anything with an implicit parameter is not a Function.
Try overloading the apply method.
scala> val sum = new Function1[Int, Function1[Int, Int]] {
| def apply(a: Int) = (b: Int) => a + b
| def apply(a: Int)(implicit b: Int) = a + b
|}
sum: java.lang.Object with (Int) => (Int) => Int{def apply(a:Int)(implicit b: Int): Int} = <function1>
scala> sum(2)(3)
res0: Int = 5
scala> implicit val b = 10
b: Int = 10
scala> sum(2)
res1: Int = 12

How to return a function in scala

How can I return a function side-effecting lexical closure1 in Scala?
For instance, I was looking at this code sample in Go:
...
// fib returns a function that returns
// successive Fibonacci numbers.
func fib() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return b
}
}
...
println(f(), f(), f(), f(), f())
prints
1 2 3 5 8
And I can't figure out how to write the same in Scala.
1. Corrected after Apocalisp comment
Slightly shorter, you don't need the return.
def fib() = {
var a = 0
var b = 1
() => {
val t = a;
a = b
b = t + b
b
}
}
Gah! Mutable variables?!
val fib: Stream[Int] =
1 #:: 1 #:: (fib zip fib.tail map Function.tupled(_+_))
You can return a literal function that gets the nth fib, for example:
val fibAt: Int => Int = fib drop _ head
EDIT: Since you asked for the functional way of "getting a different value each time you call f", here's how you would do that. This uses Scalaz's State monad:
import scalaz._
import Scalaz._
def uncons[A](s: Stream[A]) = (s.tail, s.head)
val f = state(uncons[Int])
The value f is a state transition function. Given a stream, it will return its head, and "mutate" the stream on the side by taking its tail. Note that f is totally oblivious to fib. Here's a REPL session illustrating how this works:
scala> (for { _ <- f; _ <- f; _ <- f; _ <- f; x <- f } yield x)
res29: scalaz.State[scala.collection.immutable.Stream[Int],Int] = scalaz.States$$anon$1#d53513
scala> (for { _ <- f; _ <- f; _ <- f; x <- f } yield x)
res30: scalaz.State[scala.collection.immutable.Stream[Int],Int] = scalaz.States$$anon$1#1ad0ff8
scala> res29 ! fib
res31: Int = 5
scala> res30 ! fib
res32: Int = 3
Clearly, the value you get out depends on the number of times you call f. But this is all purely functional and therefore modular and composable. For example, we can pass any nonempty Stream, not just fib.
So you see, you can have effects without side-effects.
While we're sharing cool implementations of the fibonacci function that are only tangentially related to the question, here's a memoized version:
val fib: Int => BigInt = {
def fibRec(f: Int => BigInt)(n: Int): BigInt = {
if (n == 0) 1
else if (n == 1) 1
else (f(n-1) + f(n-2))
}
Memoize.Y(fibRec)
}
It uses the memoizing fixed-point combinator implemented as an answer to this question: In Scala 2.8, what type to use to store an in-memory mutable data table?
Incidentally, the implementation of the combinator suggests a slightly more explicit technique for implementing your function side-effecting lexical closure:
def fib(): () => Int = {
var a = 0
var b = 1
def f(): Int = {
val t = a;
a = b
b = t + b
b
}
f
}
Got it!! after some trial and error:
def fib() : () => Int = {
var a = 0
var b = 1
return (()=>{
val t = a;
a = b
b = t + b
b
})
}
Testing:
val f = fib()
println(f(),f(),f(),f())
1 2 3 5 8
You don't need a temp var when using a tuple:
def fib() = {
var t = (1,-1)
() => {
t = (t._1 + t._2, t._1)
t._1
}
}
But in real life you should use Apocalisp's solution.