equal
deleted
inserted
replaced
5 // Polymorphic Types |
5 // Polymorphic Types |
6 //=================== |
6 //=================== |
7 |
7 |
8 // You do not want to write functions like contains, first, |
8 // You do not want to write functions like contains, first, |
9 // length and so on for every type of lists. |
9 // length and so on for every type of lists. |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
10 |
25 |
11 |
26 |
12 def length_string_list(lst: List[String]): Int = lst match { |
27 def length_string_list(lst: List[String]): Int = lst match { |
13 case Nil => 0 |
28 case Nil => 0 |
14 case x::xs => 1 + length_string_list(xs) |
29 case x::xs => 1 + length_string_list(xs) |
331 nfa.accepts("aaaaabbbaaa".toList) // false |
346 nfa.accepts("aaaaabbbaaa".toList) // false |
332 nfa.accepts("ac".toList) // false |
347 nfa.accepts("ac".toList) // false |
333 |
348 |
334 |
349 |
335 // Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs? |
350 // Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs? |
336 // A: Subset construction |
351 // A: Subset construction. |
337 |
352 |
338 def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = { |
353 def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = { |
339 DFA(nfa.starts, |
354 DFA(nfa.starts, |
340 { case (qs, c) => nfa.nexts(qs, c) }, |
355 { case (qs, c) => nfa.nexts(qs, c) }, |
341 _.exists(nfa.fins)) |
356 _.exists(nfa.fins)) |
403 def charlist2rexp(s: List[Char]): Rexp = s match { |
418 def charlist2rexp(s: List[Char]): Rexp = s match { |
404 case Nil => ONE |
419 case Nil => ONE |
405 case c::Nil => CHAR(c) |
420 case c::Nil => CHAR(c) |
406 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
421 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
407 } |
422 } |
408 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList) |
423 implicit def string2rexp(s: String): Rexp = |
|
424 charlist2rexp(s.toList) |
409 |
425 |
410 |
426 |
411 val r1 = STAR("ab") |
427 val r1 = STAR("ab") |
412 val r2 = STAR(ALT("ab", "baa baa black sheep")) |
428 val r2 = STAR(ALT("ab", "baa baa black sheep")) |
413 val r3 = STAR(SEQ("ab", ALT("a", "b"))) |
429 val r3 = STAR(SEQ("ab", ALT("a", "b"))) |