请选择 进入手机版 | 继续访问电脑版
楼主: ReneeBK
1334 12

[Lecture Notes]Programming Languages: Scala,Columbia University [推广有奖]

ReneeBK 发表于 2016-4-9 08:20:22 |显示全部楼层 |坛友微信交流群
  1. def simplifyTop ( expr : Expr ): Expr = expr match {
  2. case UnOp ( " -" , UnOp ( " -" , e )) = > e // Double negation
  3. case BinOp ( " + " , e , Number (0)) = > e // Adding zero
  4. case BinOp ( " * " , e , Number (1)) = > e // Multiplying by one
  5. case _ = > expr
  6. }
  7. scala > simplifyTop ( UnOp ( " -" , UnOp ( " -" , Var ( " x " ))))
  8. res0 : Expr = Var ( x )
复制代码

使用道具

ReneeBK 发表于 2016-4-9 08:20:53 |显示全部楼层 |坛友微信交流群
  1. Using tuples in pattern matching:
  2. def tupleDemo ( expr : Any ) =
  3. expr match {
  4. case (a , b , c ) = > println ( " matched " + a + b + c )
  5. case _ = > // returns Unit
  6. }
  7. scala > tupleDemo (( " a " , 3 , " - tuple " ))
  8. matched a 3 - tuple
复制代码

使用道具

AdaliaWZ 发表于 2016-6-7 12:18:27 |显示全部楼层 |坛友微信交流群
多谢分享!

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 07:17