Permalink
Browse files

Refactored patterns/fib_example to work like examples/fibonacci.

  • Loading branch information...
1 parent 6b62a4f commit 0c26c2819615d42c5a5c6f7a8cd10acbb52be100 @bakkdoor committed Apr 21, 2011
Showing with 25 additions and 12 deletions.
  1. +12 −12 lib/patterns/fib_example.fy
  2. +13 −0 lib/patterns/pattern.fy
@@ -4,24 +4,24 @@ require: "pattern"
# It's really ugly right now but we'll add some syntax sugar for it
# (See comment below)
-def fib: n {
- n case_of: (((Pattern literal: 1) || (Pattern literal: 2)) ->> { n - 1 }) otherwise: {
- n case_of: ((Pattern wildcard) ->> { fib: (n - 2) + (fib: $ n - 1) })
+class Number {
+ def fib {
+ val = self
+ self case_of: ((Pattern literal: 0) || (Pattern literal: 1) ->> { val }) otherwise: {
+ self case_of: (Pattern wildcard ->> { val - 2 fib + (val - 1 fib) })
+ }
}
}
# above should be compiled from something like this:
# def fib: n {
# match n {
-# case <1> || <2> -> n - 1
-# case <_> -> fib: (n - 2) + (fib: (n - 1))
+# case <0> || <1> -> n
+# case <_> -> n - 2 fib + (n - 1 fib)
# }
# }
-n = ARGV second to_i
-if: (n > 0) then: {
- "fib(#{n}) = " print
- fib: n . inspect println
-} else: {
- "No positive integer given!" println
-}
+15 times: |x| {
+ "fib(#{x}) = " print
+ x fib println
+}
View
@@ -86,6 +86,19 @@ class PatternNegation : Pattern {
class VariablePattern : Pattern {
def initialize: @variable {
}
+
+ def does_match: val else: fail_block {
+ if: @value then: {
+ if: (@value == val) then: {
+ Binding new: @value
+ } else: {
+ match_failed_for: val escape: fail_block
+ }
+ } else: {
+ @value = val
+ Binding new: val
+ }
+ }
}
class KeywordPattern : Pattern {

0 comments on commit 0c26c28

Please sign in to comment.