# New Syntax #64

Open
opened this Issue Nov 26, 2015 · 0 comments

Projects
None yet
1 participant
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 ``````