scala(一)/Haskel(一)

来源:互联网 发布:吉诺比利巅峰数据 编辑:程序博客网 时间:2024/06/10 04:53

在 scala里对pattern有明确的定义,在形式上有以下几种pattern:

  • 常量模式(constant patterns) 包含常量变量和常量字面量,常量必须以大写字母开头
  • 变量模式(variable patterns) ,单纯的变量模式没有匹配判断的过程,只是把传入的对象给起了一个新的变量名,要小写字母开头
  • 通配符模式(wildcard patterns),通常用于代表所不关心的部分
  • 构造器模式(constructor patterns),
  • 类型模式(type patterns),对于泛型直接用通配符替代
  • 变量绑定模式 (variable binding patterns),
  • 抽取器模式(extractor patterns)

非要把 序列模式 与构造器模式区分的话,也是从它们背后的实现上,而不是表现上。
另外《Programming in scala》一书中也单独提到 元组模式(tuple patterns),元组模式本质上也

Scala可以根据变量的内容推算其类型,叫做type inference
变量类型放在参数的后面,冒号分开
其每个表达式都有返回结果,也就是说如果函数有返回值,其后也要有冒号和变量类型
避免使用索引来枚举数组
什么是函数式风格?for comprehension
对于任何对象,后面有()就是调用了其apply方法
方法和方法之间的关联性较小,容易减少多线程的互锁问题?
在函数体内不能修改参数的值,都是val
无须;结尾,最后一行就是函数的返回值
没有返回值的函数的返回值被定义为Unit,一般是在修改成员变量的值或打印某条语句的时候。
类和其伴侣对象可以在同一个文件中访问其私有成员
脚本要求最后以表达式方式运行
singleton对象和Trait功能
隐式自动转换:当编译器看到类型X而却需要类型Y,它就在当前作用域查找是否定义了从类型X到类型Y的隐式定义

val i:Int = 3.5//出现了error type mismatch,可以用3.5.toint
有一个隐士定义:
implicit def doubleToInt(x:Double) = x toInt
doubleToInt: (x: Double)Int
那就不会报错了

其基本类型的字面量也支持方法,其实是Rich类型的方法
操作符也是普通类方法的简化
任何Scala方法可以作为操作符来使用
前、中、后缀运算符(前后是使用了unary_-方法)
如果调用方法是为了其“副作用”(),那么用;不是,就可以省
短路运算:op1||op2 op1为真则不计算op2

class rational(n:Int,d:Int)
n、d是类参数,rational是对象么?
主构造函数:代码块
用override重载基类定义的方法,而且必须使用override关键字表示重新定义基类中的成员

Application domains have distinctive/conflicting needs. There are scientific computing (Fortran), business applications (SQL), and system programming (C/C++).

read-eval-print loop:REPL
Mutability is when you decide the variable can be changed in the future. Almost all variables in Java are mutable, except for those declared as final. In C++, they are marked as const.
immutability can reduce the use of locking and increase the safety and efficiency of concurrent programming.


In purely functional programming you don’t tell the computer what to do as such but rather you tell it what stuff is
The only thing a function can do is calculate something and return it as a result;
if a function is called twice with the same parameters, it’s guaranteed to return the same result. That’s called referential transparency;
Haskell is statically typed
functions are called by writing the function name, a space and then the parameters, separated by spaces.
92 'div' 10=If a function takes two parameters, we can also call it as an infix function by surrounding it with backticks
when defining functions, there’s a = and after that we define what the function does
’ is a valid character in functions
functions can’t begin with uppercase letters
When a function doesn’t take any parameters, we usually say it’s a definition (or a name)
strings are just lists of characters
cons operator:构造操作符,[1,2,3]=1:2:3:[]

  • ‘A’:” SMALL CAT” 为什么一个单引号一个双?*
    因为small cat是一个列表,对它:t后发现是[Char],is synonymous with String

[9.4,33.2,96.2,11.2,23.25] !! 1 //index
33.2
lists有functions: head tail last init length null reverse take drop product(积) elem(infix)
Watch out when using floating point numbers in ranges
take 24 [13,26..]等同于[13,26..24*13]
range的function:cycle repeat replicate

S={2*x|x<-N}这个角度去理解comprehension(an element must satisfy all the predicates to be included in the resulting list)
后面的x<-N是predicate
The function odd returns True on an odd number and False on an even one(True就有False就无)
length’ xs = sum [1 | _ <- xs]
Nested comprehension:
let xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
ghci> [ [ x | x <- xs, even x ] | xs <- xxs]
[[2,2,4],[2,4,6,8],[2,4,2,6,2,6]]
编程思路:You take a starting set of solutions and then you apply transformations to those solutions and filter them until you get the right ones
例如:

let rightTriangles’ = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]

Everything in Haskell has a type
:t tells us its type,in the result :: is read as “has type of”

[Char] -> [Char], meaning that it maps from a string to a string
The return type is the last item in the declaration and the parameters are the first some
addThree x y z = x + y + z
addThree :: Int -> Int -> Int -> Int
Tuples are types but they are dependent on their length as well as the types of their components, so there is theoretically an infinite number of tuple types, which is too many to cover in this tutorial. Note that the empty tuple () is also a type which can only have a single value: ()

type variable.:can be of any type
Functions that have type variables are called polymorphic functions.it takes a list of any type and returns one element of that type.

面向对象编程(object-oriented programming)-oop
the equality operator, == is a function. So are +, *, -, / and pretty much all operators. If a function is comprised only of special characters, it’s considered an infix function by default. If we want to examine its type, pass it to another function or call it as a prefix function, we have to surround it in parentheses.
Everything before the => symbol is called a class constraint(类限制条件)
Eq is used for types that support equality testing
Ord is for types that have an ordering.Ordering is a type that can be GT, LT or EQ, meaning greater than, lesser than and equal, respectively.

为什么5 compare 3 我得不到GT?

type annotations:对read使用可知

:t read//为什么不加()?
read “5” :: Int //这个是可以的

0 0
原创粉丝点击