;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname lect12-scheme-versions-of-prolog) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.ss" "teachpack" "2htdp"))))) ;;;;; some scope examples: (define (make-adder n) (lambda (m) (+ n m))) ; Two examples of applying this function: ; (make-adder 4) ; evals to (lambda (m) (+ m 4)) ((make-adder 3) 4) ; evals to 7 (let {[x 5]} (+ x (let {[x 3]} x))) (let {[x 5]} (let {[x (+ x 1)]} x)) (let {[y 5]} (let {[x (+ y 1)]} (* y x))) ;;;;; Some functions to also implement in prolog: (define (my-length data) (cond [(empty? data) 0] [(cons? data) (+ (my-length (rest data)) 1)])) (define (length2 data) (length2-help 0 data)) ; Return the #items in data + len-so-far. ; (define (length2-help len-so-far data) (cond [(empty? data) len-so-far] [(cons? data) (length2-help (+ 1 len-so-far) (rest data))])) (check-expect (length2 empty) 0) (check-expect (length2 (list 'a)) 1) (check-expect (length2 (list 'a 'b)) 2) (check-expect (length2 (list 'a 'b 'c 'd 'e)) 5) (define (my-list-ref idx data) (cond [(empty? data) ...] [(cons? data) ...idx...(my-list-ref ___ (rest data))])) (check-expect (my-list-ref 0 (list 'hi)) 'hi) (check-expect (my-list-ref 0 (list 'hi 'bye 'aloha 'ciao)) 'hi) (check-expect (my-list-ref 1 (list 'hi 'bye 'aloha 'ciao)) 'bye) (check-expect (my-list-ref 3 (list 'hi 'bye 'aloha 'ciao)) 'ciao) (check-error (my-list-ref 0 empty) "idx too big") (check-error (my-list-ref 1 empty) "idx too big") (check-expect (my-list-ref 1 (list 'hi)) "idx too big") (check-expect (my-list-ref 4 (list 'hi 'bye 'aloha 'ciao)) "idx too big")