### Automatic Differentiation Example Source: https://eshkol.ai/ Demonstrates automatic differentiation to compute the derivative of a cube function at a specific point. Requires no manual calculus. ```scheme ;; Automatic differentiation — compiled to native code (define (cube x) (* x x x)) (display (derivative cube 2.0)) ;; => 12.0 -- d/dx(x^3) = 3x^2 at x=2 ``` -------------------------------- ### Knowledge Base Query Example Source: https://eshkol.ai/ Demonstrates querying a knowledge base using unification to find relationships. Asserts parent relationships and then queries for a child. ```scheme (define kb (make-kb)) (kb-assert! kb (make-fact '(parent alice bob))) (kb-assert! kb (make-fact '(parent bob charlie))) (display (kb-query kb '(parent alice ?child))) ;; => ((parent alice bob)) ``` -------------------------------- ### Continuations with Dynamic Wind Source: https://eshkol.ai/ Demonstrates the use of first-class continuations (call/cc) and dynamic-wind for managing effects during continuation capture and invocation. Guards are executed upon entering and exiting the dynamic scope. ```scheme (call/cc (lambda (k) (dynamic-wind (lambda () (display "enter ")) (lambda () (k 42)) (lambda () (display "exit"))))) ``` -------------------------------- ### Tensor Computing with Vectors and Fold Source: https://eshkol.ai/ Illustrates tensor computing by defining a vector and performing a fold operation to sum its elements. Supports various data structures and operations. ```scheme (define v #(1.0 2.0 3.0 4.0 5.0)) (display (fold-left + 0 (list 1 2 3 4 5))) ;; => 15 ``` -------------------------------- ### Neural Network Training with Gradient Descent Source: https://eshkol.ai/ Trains a simple neural network using gradient descent by defining a loss function and iteratively updating weights. Autodiff computes exact derivatives. ```scheme (define (loss w x y) (* (- (* w x) y) (- (* w x) y))) (define (train w lr data) (if (null? data) w (let ((x (caar data)) (y (cadar data))) (train (- w (* lr (derivative (lambda (w) (loss w x y)) w))) lr (cdr data))))) (display (train 0.5 0.01 '((1 2) (2 4) (3 6)))) ;; => 0.891 (converges to 2.0 over epochs) ``` -------------------------------- ### First-Class Functions: Composition Source: https://eshkol.ai/ Defines and uses function composition to create a new function that first increments a number and then doubles it. Demonstrates closures and higher-order functions. ```scheme (define (compose f g) (lambda (x) (f (g x)))) (define inc-then-double (compose (lambda (x) (* x 2)) (lambda (x) (+ x 1)))) (display (map inc-then-double '(1 2 3))) ;; => (4 6 8) ``` -------------------------------- ### Automatic Differentiation for Loss Function Source: https://eshkol.ai/ Calculates the derivative of a loss function with respect to a weight 'w' at a specific value. This showcases compiler-native AD capabilities. ```scheme (define (loss W x y) (let ((pred (* W x))) (* (- pred y) (- pred y)))) (display (derivative (lambda (w) (loss w 2.0 4.0)) 1.5)) ``` -------------------------------- ### Solve Differential Equation with RK4 Integration Source: https://eshkol.ai/ Solves a differential equation (dy/dt = -2y) using the Runge-Kutta 4th order (RK4) integration method. Reaches machine precision for e^(-4) after 200 steps. ```scheme (define (decay t y) (* -2.0 y)) (define (rk4 f t y h) (let* ((k1 (* h (f t y))) (k2 (* h (f (+ t (* 0.5 h)) (+ y (* 0.5 k1))))) (k3 (* h (f (+ t (* 0.5 h)) (+ y (* 0.5 k2))))) (k4 (* h (f (+ t h) (+ y k3))))) (+ y (* (/ 1.0 6.0) (+ k1 (* 2.0 k2) (* 2.0 k3) k4))))) (display (let loop ((t 0.0) (y 1.0) (i 0)) (if (>= i 200) y (loop (+ t 0.01) (rk4 decay t y 0.01) (+ i 1))))) ;; => 0.01832 (e^-4, machine precision) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.