hof.foldr 580 B

12345678910111213141516
  1. // foldr: right fold is a classic higher order function
  2. // It runs a function over each element in a list.
  3. //
  4. // lst: pair list, the input
  5. // fun: processes one element in the list
  6. // acc: the accumulated result, the output
  7. //
  8. (defn foldr [lst fun acc]
  9. (cond // cond is zygo's if-then-else.
  10. (empty? lst) acc // return acculated output if no more input.
  11. (fun // else call fun on the head of lst
  12. (car lst)
  13. (foldr (cdr lst) fun acc)) // recursive call on the tail of the input
  14. )
  15. )