ๆจๆฅใใใๆธใใ๏ผใฉใใช็ถๆ ใงใใกใใใจๅใ่ชไฟกใใชใ
ใใใฏใจใใใ
Haskellใงใใใใ
case list of
[] -> a
x:xs -> f x xs
ใใใใใชในใใฎใใฟใผใณใใใ. ใใกใใๅฎ้ใซใฏๆดใซ่ค้ใชใใฟใผใณใใใใใใใใฉใ ใใใฆใใฏใใฎ[]ใใฉใใใ ใใฎไบ้ใใซๅๅฒใใใใ ใใงๆธใ
ใใฟใผใณใใใใฎ็กใ่จ่ชใง็ธๅฝใฎใใจใใใใใซใฏ if ใ ๆธใใฎใงใใฃใฆใif ใฏๆฎ้ใ้ ๅปถ่ฉไพกใซใชใใฎใ ใใฉใ ไธใฎใณใผใใงใใ a ใๅฐใชใใจใใใฎๆ่ใงๅฎๆฐใงใ(x:xs)ใฎ ใใฟใผใณใฎๆใฏ(x,xs)ใๅใๅใใฉใ ใๅผใจใใใฐใใใใฏ ifใงใชใใฆใใใใใจใซใชใ๏ผ
function match(ls, u, f) {
return ls.length==0 ? u : f(ls[0], ls.slice(1));
}
ๆขใใฆๅ็บๆใใๅฟ
่ฆใใชใ length
, filter
ใๆธใใฆใฟใใจไปฅไธใฎใใใซ
function len(ls) {
return match(ls, 0, function(x,xs){ return 1+len(xs) });
}
console.log(len([1,2,3]))
function cons(x,xs) {
return [x].concat(xs);
}
function filter(ls, pred) {
return match(ls,[]
, function(x,xs){
var ys = filter(xs,pred);
return pred(x) ? cons(x, ys) : ys;
});
}
function oddp(x) { return x%2 != 0 }
console.log(filter([1,2,3,4,5], oddp))
Array#slice
ใฃใฆใฉใฎใใใใณในใๆใใใใ ใใใ