Eliza and Pair in Scala

Hello everybody,
Now I learn KI and in this case I make a program (in scala) Eliza from Joseph Weizenbaum.
For example:
package name.panitz.eliza;
class Eliza
{
import Util.{_}
val data=new Data()
var prev = “”
def ans(qs:List[String])=
{
val responses =
for (Pair(key,resp)<-data.responses
;q<-tails(qs)
;q<-isPrefix(key,q)
)
yield Pair(resp,makeResponse(resp.x.head,conjug(resp.x,q.drop(key.length))))
rotate(responses.head._1)
responses.head._2
}
}
When I run the program with sbt run .
I got this error:
[error] E:\scala\Eliza\src\main\scala\Eliza.scala:24:7: not found: value Pair
[error] for (Pair(key,resp)<-data.responses
[error] ^

Can anybody help me?
thank you and kind regards
Jürgen

Hmm – where are you getting your information from? While it’s common to use pairs in Scala, there’s no standard Pair type: a pair is usually just a 2-value Tuple, and you create it as simply (value1, value2). Is Pair supposed to be in the Util code you’re using, maybe?

In general, your code above doesn’t quite look like typical Scala (for example, those semicolons), so I’m concerned you may want to step back and learn the basics a bit more first.

this URL:
(https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjSsMndms-EAxVQ2gIHHRWTALIQFnoECA4QAQ&url=https%3A%2F%2Fwww.cs.hs-rm.de%2F~panitz%2Fki%2Fskript.pdf&usg=AOvVaw39ncDWyCOm22TcIfUaUv-2&opi=89978449)

Ah, okay – I don’t read German, so a lot of the context is lost on me, but one thing jumps out: that paper is from 2006. That is many, many, many versions of Scala ago (I’m a Scala old-timer, and that’s before I started). So in general, you should take the Scala code in that paper with a grain of salt; some of the details have evolved over the years.

Just as a style note, I’ll observe that that isn’t how you usually write for comprehensions any more. Instead of this:

for (Pair(key,resp)<-data.responses
  ;q<-tails(qs)
  ;q<-isPrefix(key,q)
) yield ...

nowadays you would more normally write:

for {
  Pair(key,resp) <- data.responses
  q <- tails(qs)
  q <- isPrefix(key,q)
} yield ...

While the parenthesized version is technically legal, it’s somewhat rare to use that form for nested comprehensions like this nowadays; I generally recommend using the curly-brace version, which lets you get rid of the semicolons.

The use of Pair there is a bit weird, but given how ancient a version of Scala that must be, it’s possible that Pair was a built-in type back then. Nowadays it doesn’t exist, at least in the standard library; instead, like I mentioned above, you would just specify it as a Tuple, by putting the values in parentheses. (Parens are special in that way: they mean Tuple, and a “pair” is just a 2-valued Tuple.)

Good luck, and come back if you hit other problems!

1 Like

Hello jducoeu,
thank you for help. I must try to find the solution.
bye bye Jürgen

In Scala 2.0.0, which was released in early 2006 (so around the time that your paper was written), Pair was an alias for Tuple2 - see this old Scala 2.0.0 version of scala.Predef.

Okay, I was wondering if that might be the case. In which case, the clause in question would today be written as:

val responses =
  for {
    (key, resp) <- data.responses
    q <- tails(qs)
    q <- isPrefix(key,q)
  } yield (resp, makeResponse(resp.x.head, conjug(resp.x, q.drop(key.length))))

Basically, since Pair is just Tuple2, and nowadays you represent tuples with plain parens, you can generally just drop the word Pair and it should be right.

Pair was deprecated for 2.11.

The comment was that there were “too many ways of doing things”, though with (x,y) syntax there is still a pair of ways (two ways).