/** * Beispiel aus * * - Algorithmen und Datenstrukturen für Dummies * - von Andreas Gogol-Döring und Thomas Letschert * - Verlag Wiley-VCH; Oktober 2019 * - Kapitel 3, Daten und ihre Struktur * * @author A. Gogol-Döring, Th. Letschert */ import Foundation struct AuD_03_10_Traversing { static let Add: (Int, Int) -> Int = { (x: Int, y: Int) -> Int in x + y } static func TraverseWith(lst: [Int], f: (Int, Int) -> Int, start: Int) -> Int { guard let head = lst.first else { return start } let rest = Array(lst.dropFirst()) return f(head, TraverseWith(lst: rest, f: f, start: start)) } static func ListSum(lst: [Int]) -> Int { return TraverseWith(lst: lst, f: Add, start: 0) } static func run() { let lst = [1,2,3,4,5] print(ListSum(lst: lst)) } }