PasteRack.org
Paste # 10933
2023-05-18 03:07:26

Fork as a new paste.

Paste viewed 1195 times.


Embed:

lambda mayhem

  1. #lang racket
  2. (define brainteaser
  3.   (foldl (λ (i g)            ; procedure
  4.            (λ (y)
  5.              (g (+ y i))))
  6.          (λ (x) x)           ; initial value
  7.          (list 1 2 3 4)      ; list
  8.    )
  9. )
  10.  
  11. (println (brainteaser 5))
  12.  
  13. ;
  14. ;
  15. ; my interpretation:
  16. (((λ (i g) (λ (y) (g (+ y i)))) 4   ((λ (i g) (λ (y) (g (+ y i)))) 3  ((λ (i g) (λ (y) (g (+ y i)))) 2  ((λ (i g) (λ (y) (g (+ y i)))) 1  (λ (x) x)))))  5)
  17.                                                                                                                                       ;i ;<g-----g>     ;y
  18.                                                                                                         => 5 + 1
  19.                                                                                                     ;i ;<g----------------------------------------g>    ;y
  20.                                                                         => 6 + 2
  21.                                                                   ;i ;<g---------------------------------------------------------------------------g>   ;y
  22.                                       => 8 + 3
  23.                                ;i  ;<g--------------------------------------------------------------------------------------------------------------g>  ;y
  24.    => 11 + 4

=>

15

15