8. . . . . . .
樂透組合 Python 版
V,K,T,M = 49 , 6 , 3 , 6
mn, num = 1 , 0
ok = lambda l1 , l2 : sum( i in l2 for i in l1 ) >= T
A = l i s t ( combinations ( range (1 ,V+1) , M) )
A2 = l i s t ( combinations ( range (1 ,V+1) , K) )
while mn:
num+=1
mn, mc = max((sum(ok( l , i ) for i in A) , l ) for l in A2)
A = f i l t e r (lambda x : not ok(x ,mc) , A)
pr int ”%s mc=%d mn=%d”%(num, mc, mn)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 8 / 50
9. . . . . . .
樂透組合 F# 版
l e t V,K,T,M = 49 , 6 , 3 , 6
l e t mn, num= r e f 1 , r e f 0
l e t ok l1 l2 = (sum [ for i in l1 −> Int ( e x i s t s i l2 ) ] ) >= T
l e t A = combinations [1 . . V] M |> r e f
l e t A2 = combinations [1 . . V] K
while !mn >0 do
num := !num + 1
l e t mn’ , mc = max[ for l in A2 −> (sum[ for i in !A −> Int (ok l i ) ] , l ) ]
A := f i l t e r (( ok mc)>>not ) !A
mn := mn’
p r i n t f n ”%A mc=%A mn=%A” !num mc !mn
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 9 / 50
10. . . . . . .
尋找理想語言
I was asked a few weeks ago, ”What was the biggest surprise
you encountered rolling out Go?”
I knew the answer instantly: Although we expected C++
programmers to see Go as an alternative, instead most Go
programmers come from languages like Python and Ruby. Very
few come from C++. – Rob Pike *
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 10 / 50
11. . . . . . .
The Zen of Python
>>> import this The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity. Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 11 / 50
12. . . . . . .
The Zen of Python
易讀 Readability counts
易維護 Easy to maintain
易實作 Easy to implement
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 12 / 50
13. . . . . . .
Language Matters
h =
0◦ if max = min
60◦ × g−b
max−min + 0◦, if max = r and g ≥ b
60◦ × g−b
max−min + 360◦, if max = r and g < b
60◦ × b−r
max−min + 120◦, if max = g
60◦ × r−g
max−min + 240◦, if max = b
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 13 / 50
31. . . . . . .
先講結論
Go
適合 Web Service、標準演算法、多工
適合用 C 的方式思考的
不適合科學計算,想客製化語言的人
F# 和 Scala 差不多,看你比較討厭 { 還是 MS
適合常用 Functional Programming 方式使用 Python
適合喜歡客製化語言的人
常用 OOP 而且對抽象化接受度高者,適合 Scala
F# 傾向重新定義「語意」的方式來客製化。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 19 / 50
32. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
33. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
34. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
35. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
36. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
37. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
38. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
39. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
40. . . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to Go from Python. Surprised to see a
reduction in line counts.”—Scott Dunlop
~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of
Catch.com
“[Go] is the most elegant imperative language ever (including dynamic
ones like Lua, Ruby, and Python)”—Quoc Anh Trinh
把 Python 跟 Go 結合在一起有沒有搞頭?
就叫做 PyGon 好了 (名字都取好了)
先用 Ply 寫個 Go 的 Parser 好了。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
41. . . . . . .
Go
沒有 Class
沒有 template, function generics
沒有 operator overloading
沒有 list comprehension
“I thought I would miss list comprehensions a lot, but I find
that their absence results in cleaner code that is easier to
understand even when compared to relatively simple list
comprehensions [in Python].”—Thirsteh
沒有分號,但還是有很多 {
沒有 Exception
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 21 / 50
42. . . . . . .
Go
Garbage collected
Interface 有 duck type 的特點
有 Go Playground 和 A Tour of Go *
自動排版
不准有 unused variable
有 go
特別的錯誤處理方式
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 22 / 50
48. . . . . . .
Go Example 1: map
package main
import ”fmt”
type Vertex struct {
Lat , Long float64
}
var m = map[ s t r i n g ] Vertex{
” Bell Labs” : {40.68433 , −74.39967},
”Google” : {37.42202 , −122.08408},
}
func main () {
v , ok := m[ ”Moon” ]
fmt . Println (”The value : ” , v , ” Present ?” , ok)
m[ ”Moon” ] = Vertex {0 , 0}
for k , v := range m {
fmt . Println (k , v , m[ k ] )
}
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 25 / 50
49. . . . . . .
Go Example 2: For
var pow = [ ] i n t {1 , 2 , 4 , 8 , 16 , 32 , 64 , 128}
func main () {
for i , v := range pow {
fmt . Printf (”2**%d = %dn” , i , v)
}
for _, value := range pow {
fmt . Printf (”%dn” , value )
}
for i := 0; i < len (pow) ; i++ {
fmt . Printf (”%d , %dn” , i , pow[ i ] )
}
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 26 / 50
50. . . . . . .
Go Example 3: Switch
func main () {
fmt . Println (”When ’ s Saturday?”)
today := time .Now() . Weekday()
switch time . Saturday {
case today + 0:
fmt . Println (”Today . ”)
case today + 1:
fmt . Println (”Tomorrow . ”)
case today + 2:
fmt . Println (” In two days . ”)
default :
fmt . Println (”Too fa r away . ”)
}
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 27 / 50
51. . . . . . .
Go Example 4: method
type Vertex struct {
X, Y float64
}
func (v *Vertex ) Abs () float64 {
return math . Sqrt (v .X*v .X + v .Y*v .Y)
}
type MyFloat float64
func ( f MyFloat ) aa () float64 {
return f + 2.3
}
func main () {
v := &Vertex {3 , 4}
fmt . Println (v . Abs () )
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 28 / 50
52. . . . . . .
Go Example 5: interface
type Writer i n t e r f a c e {
Write (b [ ] byte ) (n int , er r er r o r )
}
func write (w Writer , s t r s t r i n g ) {
w. Write ( [ ] byte ( s t r ) )
}
func main () {
var w Writer
// os . Stdout implements Writer
w = os . Stdout
fmt . Fprintf (w, ” hello , writer n”)
write ( os . Stdout , ” tes t n”)
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 29 / 50
53. . . . . . .
Error
An error is anything that can describe itself as an error string.
type e r r o r i n t e r f a c e {
Error () s t r i n g
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 30 / 50
54. . . . . . .
Go Example 6: HTTP Server
package main
import (
”fmt”
”net/http ”
)
type Hello struct {}
func (h Hello ) ServeHTTP(
w http . ResponseWriter ,
r *http . Request ) {
fmt . Fprint (w, ” Hello ! ”)
}
func main () {
var h Hello
http . ListenAndServe (” lo c a l h o s t :4000” , h)
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 31 / 50
55. . . . . . .
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
go f ( x , y , z )
Channels
ch <− v // Send v to channel ch .
v := <−ch // Receive from ch , and
// a s s i g n value to v .
類似 python 的 multiprocessing/pipe,但更好,取代 generator 的功能。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 32 / 50
56. . . . . . .
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
go f ( x , y , z )
Channels
ch <− v // Send v to channel ch .
v := <−ch // Receive from ch , and
// a s s i g n value to v .
類似 python 的 multiprocessing/pipe,但更好,取代 generator 的功能。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 32 / 50
57. . . . . . .
Go Example 7:Goroutines
func sum(a [ ] int , c chan i n t ) {
sum := 0
for _, v := range a {
sum += v
}
c <− sum // send sum to c
}
func main () {
a := [ ] i n t {7 , 2 , 8 , −9, 4 , 0}
c := make( chan i n t )
go sum(a [ : len (a) /2] , c )
go sum(a [ len (a) /2:] , c )
x , y := <−c , <−c // r ec e i ve from c
fmt . Println (x , y , x+y)
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 33 / 50
58. . . . . . .
Go Example 8:Goroutines
func main () {
t i c k := time . Tick (100 * time . Millisecond )
boom := time . After (500 * time . Millisecond )
for {
s e l e c t {
case <−t i c k :
fmt . Println (” t i c k . ”)
case <−boom:
fmt . Println (”BOOM! ”)
return
default :
fmt . Println (” . ”)
time . Sleep (50 * time . Millisecond )
}
}
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 34 / 50
59. . . . . . .
F#
定義函數
l e t add x y =
x + y
// v a l add : x : i n t −> y : i n t −> i n t
add 2 2
l e t add ( x , y ) =
x + y
// v a l add : x : i n t * y : i n t −> i n t
add (2 ,2)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 35 / 50
60. . . . . . .
F#
l e t id x =
x
// v a l id : x : ’ a −> ’ a
id 3L
// v a l i t : int64 = 3L
id ” coscup ”
// v a l i t : s t r i n g = ” coscup ”
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 36 / 50
61. . . . . . .
F#
標記型態
l e t add ( x : int64 ) y =
x + y
// v a l add : x : int64 −> y : int64 −> int64
l e t add x y =
( x : int64 ) + y
// v a l add : x : int64 −> y : int64 −> int64
l e t add = ( fun x y −> x + y )
// val add : x : i n t −> y : i n t −> i n t
l e t i n l i n e add x y = x+y ; ;
(*
val i n l i n e add :
x : ^a −> y : ^b −> ^c
when ( ^a or ^b) : ( s t a t i c member ( + ) : ^a * ^b −> ^c )
*)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 37 / 50
62. . . . . . .
F#
標記型態
l e t add ( x : int64 ) y =
x + y
// v a l add : x : int64 −> y : int64 −> int64
l e t add x y =
( x : int64 ) + y
// v a l add : x : int64 −> y : int64 −> int64
l e t add = ( fun x y −> x + y )
// val add : x : i n t −> y : i n t −> i n t
l e t i n l i n e add x y = x+y ; ;
(*
val i n l i n e add :
x : ^a −> y : ^b −> ^c
when ( ^a or ^b) : ( s t a t i c member ( + ) : ^a * ^b −> ^c )
*)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 37 / 50
63. . . . . . .
F#
Currying
l e t inc = add 1
l e t z = inc 6
Operator overloading
l e t (| >) x f = f x
l e t (^?) a b = Regex . IsMatch (a , b)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 38 / 50
64. . . . . . .
F#
Currying
l e t inc = add 1
l e t z = inc 6
Operator overloading
l e t (| >) x f = f x
l e t (^?) a b = Regex . IsMatch (a , b)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 38 / 50
65. . . . . . .
F#
type Book =
{ Name : s t r i n g ;
AuthorName : s t r i n g ;
Rating : i n t ;
ISBN : s t r i n g }
l e t expertFSharp =
{ Name = ” Expert F#” ;
AuthorName = ”Don Syme” ;
Rating = 5;
ISBN = ” 1590598504 ” }
(另外關於 “class” 語法可看 *)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 39 / 50
66. . . . . . .
F#
type Book =
{ Name : s t r i n g ;
AuthorName : s t r i n g ;
Rating : i n t ;
ISBN : s t r i n g }
l e t expertFSharp =
{ Name = ” Expert F#” ;
AuthorName = ”Don Syme” ;
Rating = 5;
ISBN = ” 1590598504 ” }
(另外關於 “class” 語法可看 *)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 39 / 50
67. . . . . . .
F#
Pattern Matching
l e t input = [ ( 1 . , 2. , 0 . ) ; ( 2 . , 1. , 1 . ) ; ( 3 . , 0. , 1 . ) ]
l e t rec search l s t =
match l s t with
| ( 1 . , _, z ) : : t a i l −>
p r i n t f n ” found x=1. and z=%f ” z ; search t a i l
| ( 2 . , _, _) : : t a i l −>
p r i n t f n ” found x=2.” ; search t a i l
| _ : : t a i l −> search t a i l
| [ ] −> p r i n t f n ”done . ” search input
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 40 / 50
68. . . . . . .
F#
Comprehension
l e t products = [ f o r x in vec1 do f o r y in vec2 do y i e l d x*y ]
l e t vseq = seq { f o r x in 1 . . 10 −> x}
Seq .map ( fun x −> x*x ) vseq
// val i t : seq<int > = seq [ 1 ; 4; 9; 16; . . . ]
vseq |> Seq .map ( fun x −> x*x )
vseq |> PSeq .map ( fun x −> x*x ) |> Array . ofSeq
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 41 / 50
69. . . . . . .
F#
Comprehension
l e t products = [ f o r x in vec1 do f o r y in vec2 do y i e l d x*y ]
l e t vseq = seq { f o r x in 1 . . 10 −> x}
Seq .map ( fun x −> x*x ) vseq
// val i t : seq<int > = seq [ 1 ; 4; 9; 16; . . . ]
vseq |> Seq .map ( fun x −> x*x )
vseq |> PSeq .map ( fun x −> x*x ) |> Array . ofSeq
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 41 / 50
70. . . . . . .
比較
Haskell vs. F# vs. Scala: A High-level Language Features and Parallelism
Support Comparison
Prabhat Totoo, Pantazis Deligiannis, Hans-Wolfgang Loidl. *
漂亮的程式碼 (pure),速度為醜的程式碼的
49.3% for Haskell
33.8% for F#
57.5% in Scala.
F# 在 Linux 上最慢,但在 Windows 上最快 (記憶體也用最少),。
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 42 / 50
71. . . . . . .
Compare: fib
def f i b (n) :
return 1 i f n < 2 e l s e f i b (n−1) + f i b (n−2)
for n in range (40) :
prin t f i b (n)
l e t rec f i b n =
i f n < 2 then 1L e l s e f i b (n−1) + f i b (n−2)
for n = 0 to 39 do
p r i n t f n ”%A” ( f i b n)
package main
import ”fmt”
func f i b (n i n t ) i n t {
i f n < 2 {
return 1
}
return f i b (n−1) + f i b (n−2)
}
func main () {
for n := 0;n<40;n++ {
fmt . Println ( f i b (n) )
}
} TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 43 / 50
72. . . . . . .
Compare: fib
import sys
def f i b (n) :
return 1 i f n < 2 e l s e f i b (n−1) + f i b (n−2)
N=i n t ( sys . argv [ 1 ] )
for n in range (N) :
prin t f i b (n)
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 44 / 50
73. . . . . . .
Compare: fib
l e t rec f i b n =
i f n < 2 then 1L e l s e f i b (n−1) + f i b (n−2)
[<EntryPointAttribute >]
l e t main args =
l e t N = System . Convert . ToInt32 ( args . [ 0 ] )
for n = 0 to N−1 do
f i b n |> p r i n t f n ”%A”
0
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 45 / 50
74. . . . . . .
Compare: fib
package main
import (
”fmt”
” strconv ”
”os”
)
func f i b (n i n t ) i n t {
i f n < 2 {
return 1
}
return f i b (n−1) + f i b (n−2)
}
func main () {
N, _ := strconv . Atoi ( os . Args [ 1 ] )
for n := 0;n<N; n++ {
fmt . Println ( f i b (n) )
}
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 46 / 50
75. . . . . . .
Compare: fib
object f i b {
def f i b (n : Int ) : Long = i f (n < 2) 1 e l s e f i b (n−1)+f i b (n−2)
def main( args : Array [ String ] ) {
var n=0;
var N=args (0) . toInt
for (n <− 0 to N−1){
p r i n t l n ( f i b (n) )
}
}
}
TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 47 / 50