ML like pattern matching