# HG changeset patch # User Christian Urban # Date 1477306727 -3600 # Node ID 890188804fb403374db38b32945b06ac56c71b47 # Parent 780486571e38179eccd174a10f39b5d94bb30008 updated diff -r 780486571e38 -r 890188804fb4 hws/hw06.pdf Binary file hws/hw06.pdf has changed diff -r 780486571e38 -r 890188804fb4 progs/comb1.scala --- a/progs/comb1.scala Sat Oct 22 15:18:11 2016 +0100 +++ b/progs/comb1.scala Mon Oct 24 11:58:47 2016 +0100 @@ -5,42 +5,34 @@ * I <% Seq[_] , which means that the input type I can be * treated, or seen, as a sequence. */ -trait Input { - def isEmpty: Boolean -} - +abstract class Parser[I <% Seq[_], T] { + def parse(ts: I): Set[(T, I)] -abstract class Parser[T] { - - def parse(ts: Input): Set[(T, Input)] - - def parse_all(ts: Input) : Set[T] = + def parse_all(ts: I) : Set[T] = for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head } -class SeqParser[T, S](p: => Parser[T], - q: => Parser[S]) extends Parser[(T, S)] { - def parse(sb: Input) = +class SeqParser[I <% Seq[_], T, S](p: => Parser[I, T], + q: => Parser[I, S]) extends Parser[I, (T, S)] { + def parse(sb: I) = for ((head1, tail1) <- p.parse(sb); (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2) } -class AltParser[T](p: => Parser[T], - q: => Parser[T]) extends Parser[T] { - def parse(sb: Input) = p.parse(sb) ++ q.parse(sb) +class AltParser[I <% Seq[_], T](p: => Parser[I, T], + q: => Parser[I, T]) extends Parser[I, T] { + def parse(sb: I) = p.parse(sb) ++ q.parse(sb) } -class FunParser[T, S](p: => Parser[T], - f: T => S) extends Parser[S] { - def parse(sb: Input) = +class FunParser[I <% Seq[_], T, S](p: => Parser[I, T], + f: T => S) extends Parser[I, S] { + def parse(sb: I) = for ((head, tail) <- p.parse(sb)) yield (f(head), tail) } // atomic parsers -case class CharParser(c: Char) extends Parser[Char] { - type Input = String - +case class CharParser(c: Char) extends Parser[String, Char] { def parse(sb: String) = if (sb.head == c) Set((c, sb.tail)) else Set() } diff -r 780486571e38 -r 890188804fb4 style.sty --- a/style.sty Sat Oct 22 15:18:11 2016 +0100 +++ b/style.sty Mon Oct 24 11:58:47 2016 +0100 @@ -57,7 +57,7 @@ \newcommand{\HEADER}{{\bf Please submit your solutions via email. Please submit only ASCII text or PDFs. Every solution should be preceeded by the corresponding -question, like: +question text, like: \begin{center} \begin{tabular}{ll}