view code/ll.lm @ 85:f420fabd0e44 default tip

One last README change
author Michael Pavone <pavone@retrodev.com>
date Mon, 28 Jul 2014 04:42:24 -0700
parents d5ccb66ae98b
children
line wrap: on
line source

#{
	length <- :lst {
		len <- []
		while: { not: (lst isInteger?)} do: {
			lst <- lst tail
			len <- len + 1
		}
		len
	}
	
	reverse <- :lst {
		new <- []
		while: { not: (lst isInteger?)} do: {
			new <- (lst value) | new
			lst <- lst tail
		}
		new
	}

	split:at <- :lst :pos {
		first <- []
		i <- 0
		while: { i < pos } do: {
			first <- (lst value) | first
			lst <- lst tail
			i <- i + 1
		}
		#[(reverse: first) lst]
	}
	
	map <- :lst fun {
		new <- []
		while: { not: (lst isInteger?) } do: {
			new <- (fun: (lst value)) | new
			lst <- lst tail
		}
		reverse: new
	}
	
	fold:with <- :lst acc :fun {
		while: { not: (lst isInteger?) } do: {
			acc <- fun: acc (lst value)
			lst <- lst tail
		}
		acc
	}
	
	filter <- :lst pred {
		new <- []
		while: { not: (lst isInteger?) } do: {
			if: (pred: (lst value)) {
				new <- (lst value) | new
			} else: {}
			lst <- lst tail
		}
		reverse: new
	}

	flatten <- :lst {
		fold: lst [] with: :acc el {
			fold: el acc with: :iacc iel {
				iel | iacc
			}
		}
	}
}