New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Syntax #64

Open
egisatoshi opened this Issue Nov 26, 2015 · 0 comments

Comments

Projects
None yet
1 participant
@egisatoshi
Member

egisatoshi commented Nov 26, 2015

The following code is an idea of new syntax for version 4.0.

nats = 1:map($n -> n + 1, nats)

nats = 1:map($ + 1, nats)

primes = filter(prime?, nats)

matchAll [1, 2, 3] as multiset(integer) with
  $x:x:_ -> x
end

matchAll [1, 2, 3] as multiset(integer) with
  $x:x:_ -> x
end

twinPrimes =
  matchAll primes as list(integer) with
    _ ++ $p : (p + 2) : _ -> (p, p + 2)
  end

function fact(n) =
  foldl(*, 1, 1..n)
end

fact(n) = foldl(*, 1, 1..n)

function gcd(m,n)
  if (m >= n)
    if (n == 0)
      m
    else
      gcd(n,m % n)
    end
  else
    gcd(n,m)
  end
end

function poker(cards)
  match cards as multiset(card) with
    | Card($s, $n):
       Card(s, n - 1):
        Card(s, n - 2):
         Card(s, n - 3):
          Card(s, n - 4):[]
       -> StraightFlush
    | Card(_, $n):
       Card(_, n):
        Card(_, n):
         Card(_, n):
          _:[]
       -> FourOfKind
      ...
  end
end

matcher multiset(e)
  | $:$ as [e, multiset(e)] ->
      | $tgt -> matchAll tgt as list(e) with
                  $hs ++ $x : $ts -> (x, hs ++ ts)
                end
  | x as [] ->
      | $tgt -> match (x, tgt) as (list(e), multiset(e)) with
                  | ($y:$ys, y:ys) -> [()]
                  | _ -> []
                end
  | $ as [something] ->
      | $tgt -> [tgt]
end

pattern-function twin(p1, p2) =
  (~p1 & $p):p:~p2
end

function mahjongComplete?(tiles)
  match tiles as multiset(tile) with
    | twin($t_1, twin($t_2, twin($t_3, ...))) -> True
    | ...
    | _ -> False
  end
end

@egisatoshi egisatoshi added this to the 4.0.0 milestone Nov 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment