読者です 読者をやめる 読者になる 読者になる

taketoncheir.log

Like the Decatoncheir by Poseidon Industrial, This blog is Yet Another Storage for My Long Term Memories.

Functional Programming Principles in Scala in 2013/Sep Week 2

week 2の備忘録

# Higher Order Functions

  • とりあえず、こいつがScalaのEBNF (Extended Backus-Naur form)だ
  • | はalternative, [...]はoption, {...}はrepetitionを表す
  • varがない。functionalな部分について、ということか?
// Types

Type         = SimpleType
             | FunctionType
FunctionType = SimpleType '=>' Type
             | '(' [Types] ')' '=>' Type
SimpleType   = Ident
Types        = Type {',' Type}

// Expressions

Expr         = InfixExpr
             | FunctionExpr
             | if '(' Expr ')' Expr else Expr
InfixExpr    = PrefixExpr
             | InfixExpr Operator InfixExpr
Operator     = Ident
PrefixExpr   = ['+' | '-' | '!' | '~'] SimpleExpr
SimpleExpr   = ident
             | literal
             | SimpleExpr '.' ident
             | Block
FunctionExpr = Bindings '=>' Expr
Bindings     = ident [':' SimpleType]
             | '(' [Binding {',' Binding }] ')'
Binding      = ident [':' Type]
Block        = '{' {Def ';'} Expr '}'

// Definitions

Def          = FunDef | ValDef
FunDef       = def ident {'(' [Parameters] ')'} [':' Type] '=' Expr
ValDef       = val ident [':' Type] '=' Expr
Parameter    = ident ':' [ '=>' ] Type
Parameters   = Parameter {',' Parameter }
  • (x: Int)はcall-by-value, (x: => Int)はcall-by-name
  • classで型宣言できる
class Rational(x: Int, y: Int) {
  def numer = x
  def denom = y
}