#lang racket ;; higherorder.rkt ;; CS 313 - higher-order functions ; compute sum of (f x) for x = lo .. hi, incrementing x by calling (next x) (define (sumfun f next lo hi) (if (> lo hi) 0 (+ (f lo) (sumfun f next (next lo) hi)))) (define (square x) (* x x)) (define (add1 x) (+ x 1)) ; (sumfun square add1 3 6) ; using lambda: ; (sumfun (lambda (x) (* x x)) ; (lambda (x) (+ x 1)) ; 3 6) ; 'map' the function f to each element in mylist ; (equivalent to the built-in function map) (define (mymap f mylist) (if (null? mylist) '() (cons (f (car mylist)) (mymap f (cdr mylist))))) ; (mymap square '(1 2 3 10)) ; -> '(1 4 9 100) ; (mymap (lambda (x) (+ x 10)) '(1 2 3 10)) ; -> '(11 12 13 20)