concat :: [[a]] -> [a]ghci> concat [[1,2,3], [4,5,6]][1,2,3,4,5,6]它去除了一层嵌套。ghci> concat [[[1,2],[3]], [[4],[5],[6]]][[1,2],[3],[4],[5],[6]]ghci> concat (concat [[[1,2],[3]], [[4],[5],[6]]])[1,2,3,4,5,6]reverse函数返回...
getLine是一个读取一行的 I/O action。这是两个非常直觉的函式,多数程式语言也有类似这两个函式的 statement 或 function。但现在我们来看看getContents。getContents是一个从标准输入读取直到 end-of-file 字元的 I/O action。他的型态是getContents :: IO String。最酷的是getContents是惰性 I/O (Lazy I/O...
main=mainWith myFunctionwheremainWith function =doargs<-getArgscaseargs of [input, output]->interactWith function input output _-> putStrLn"error: two parameters needed"myFunction= tail 注意每一行的字符缩进。 这段代码中myFunction就是对输入文件的内容进行处理的函数,这里是tail函数,也可以改成其他的...
两者是等价的, 满足结合律. 幺半群(Monoid) 幺半群, 在半群的基础上增加一个条件, 存在幺元, 幺元跟任何元素a结合得到的都是a本身, 例子的话, 在上面这个非空字符串的例子当中再加上空字符串, 那么"" `concat` "a"得到"a", 然后"a" `concat` ""得到"a", 两者等价的,""就是这个集合的幺元. 群...
function flip(f, a, b){ return f(b,a); } //这个函数需要进行柯里化,否则无法在foldr中作为参数传入 var flip_ = _.curry(flip); function cons(a,b){ return a.concat(b); } function reverse(list){ return foldr(flip_(cons),[],list); ...
function apply(f) { var args = Array.prototype.slice.call(arguments, 1) return function(x) { return f.apply(null, args.concat(x)) } } function propertyEquals(propertyName, value, obj) { return (obj[propertyName] == value) } count([{name:"bob"},{name:"john"}], apply(propertyEqua...
functionapply(f){varargs=Array.prototype.slice.call(arguments,1)returnfunction(x){returnf.apply(null,args.concat(x))}}functionpropertyEquals(propertyName,value,obj){return(obj[propertyName]==value)}count([{name:"bob"},{name:"john"}],apply(propertyEquals,"name","bob"))// == 1 ...
haskell简介 2009/3/27 Haskell简介 Liu Jian (liujian@nfschina.com) 中国科学院软件研究所 Institute of Software,Chinese Academy of Sciences
还可以 dependent generator:[(x, y) | x <- [1..3], y <- [x..3]];concat :: [[a]] -> [a],concat xss = [x | xs <- xss, x <- xs]。 还可以 guards。[x | x <- [1..10], even x]。 zip 很有用,例如可以获取下标: ...
高阶函数(Higher-order function) 柯里化(Currying) 闭包(Closure) 函数式编程的优点 函数式的最主要的好处主要是不可变性带来的。没有可变的状态,函数就是引用透明(Referential transparency)的和没有副作用(No Side Effect)。 函数即不依赖外部的状态也不修改外部的状态,函数调用的结果不依赖调用的时间和位置,这样...