Created
December 4, 2013 19:34
-
-
Save jbclements/7794048 to your computer and use it in GitHub Desktop.
example of using 'foldl' to eliminate nested calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; a state is a number | |
;; add the two numbers to the state | |
(define (state-update state a b) | |
(+ state a b)) | |
(define my-state 234) | |
;; either: | |
(state-update | |
(state-update | |
(state-update | |
(state-update | |
(state-update | |
(state-update | |
(state-update | |
my-state 0 15) 1 79) 7 98) 8 34) 8 97) 4 87) 10 278) | |
;; OR | |
(define (state-update/l input-list state) | |
(state-update state (first input-list) (second input-list))) | |
(foldl | |
state-update/l | |
my-state | |
(list (list 0 15) | |
(list 1 79) | |
(list 7 98) | |
(list 8 34) | |
(list 8 97) | |
(list 4 87) | |
(list 10 278))) | |
;; OR, even shorter, using list abbreviations | |
(foldl | |
state-update/l | |
my-state | |
'((0 15) | |
(1 79) | |
(7 98) | |
(8 34) | |
(8 97) | |
(4 87) | |
(10 278))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment