# NOT RUN {
linked_list := NIL | CONS(car, cdr : linked_list)
lst <- CONS(1, CONS(2, CONS(3, NIL)))
len <- function(lst, acc = 0) {
cases(lst,
NIL -> acc,
CONS(car,cdr) -> len(cdr, acc + 1))
}
len(lst)
list_sum <- function(lst, acc = 0) {
cases(lst,
NIL -> acc,
CONS(car,cdr) -> list_sum(cdr, acc + car))
}
list_sum(lst)
# }
Run the code above in your browser using DataLab