####function主體結(jié)構(gòu) function.name <- function(arguments) { computations on the arguments some other code }
` mycal<-function(x){ mean<-mean(x) sd<-sd(x) result <- list(mean=mean,sd=sd) return(result) } x<-c(1,2,3,80) mycal(x) mysave<-function(earn,spend,lost){ save<- earn-spend-lost result <- list(save=save) return(result) } mysave(earn = 1000,spend = 80,lost = 10) myscore<-function(paper,attendence){ score<- paper*0.7+attendence*.3 result <- list(score=score) return(result) } myscore(paper = 92,attendence = 70)
函數(shù)式編程語言不同于大多數(shù)人所熟知的編程范式,它沒有了維護(hù)全局狀態(tài)的麻煩,只需要將輸入數(shù)據(jù)傳給函數(shù),然后等待輸出結(jié)果,就這么簡單。
Simon 說,“大部分編程語言都是命令式(imperative)的,程序員需要告訴代碼先做什么再做什么。而函數(shù)式編程語言則不然,它會直接告訴程序員輸出的結(jié)果是什么。比如 Excel 表格里的方程式,它并不包含一系列的步驟,它只會告訴你某個格子經(jīng)過計算之后的值是多少”。
|