scala 初识

var    声明可变的变量

val    不可变常量,一旦初始化,就不能改变


var a:Int  就确切的指明了类型Int,而不采用类型推断


for(a <- 1 until 3;b<- 1 to 3) println(a," ",b)

结果

1 1

1 2

1 3

2 1

2 2

2 3


0.2e-2     0.002

2e2         200


def   定义function

creates a function that returns the max value for two integer values. 

def max(x: Int, y: Int): Int = {
if (x > y) x
else y
}

通过递归,求最大公约数

def gcd(x: Long, y: Long): Long =
if (y == 0) x else gcd(y, x % y)


def gcd(x:Long,y:Long):  Long {


}

s  match {

case "one" =>  1

case "two" =>   2

case  _      =>   0

}

function函数也是对象,函数本身也能被当做对象参数 传递 进 function

def filter(inLst:List[Int],cond:(Int)=>Boolean):List[Int]={
  if(inLst==Nil) Nil
  else if(cond(inLst.head)) inLst.head::filter(inLst.tail,cond)
  else filter(inLst.tail,cond)
}
filter(lst,(v:Int)=> v%2==0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值