本文写于 2020年12月29日,距今已超过 1 年,距 2021年01月11日 的最后一次修改也已超过 3 个月,部分内容可能已经过时,您可以按需阅读。如果图片无法显示或者下载链接失效,请给我反馈,谢谢!


5 1 投票
评分

list-nth-mod: Incorrect implementation for n=0 (Result of (list-nth-mod (quote (-5)) 0) was expected to equal -5) [incorrect answer]

list-nth-mod: Exception for empty list (No exception raised) [incorrect answer]

With extra credit you received 102.0%! Grade capped at 100%

#lang racket

(provide (all-defined-out)) ;; so we can put tests in a second file

;; put your code below

(define (sequence low high stride)
  (if (> low high)
      null
      (cons low
            (sequence (+ low stride) high stride))))


(define (string-append-map xs suffix)
  (map (lambda (x) (string-append x suffix)) xs))


(define (ith xs i)
  (if (= i 0)
      (car xs)
      (ith (cdr xs) (- i 1))))

(define (list-nth-mod xs n)
  (cond [(null? xs) error "list-nth-mod: empty list"]
        [(< (car xs) 0) error "list-nth-mod: negative number"]
        [#t (ith xs (remainder n (length xs)))]))


(define (stream-for-n-steps s n)
  (if (= n 0)
      null
      (cons (car (s)) (stream-for-n-steps (cdr (s)) (- n 1)))))


(define (stream-maker f g args)
  (letrec ([fn (lambda (x) (cons (f x) (lambda () (fn (g x)))))])
    (lambda () (fn args))))


(define funny-number-stream
  (stream-maker (lambda (x)
                  (if (= (remainder x 5) 0)
                      (- 0 x)
                      x))
                (lambda (x)
                  (+ x 1))
                1))


(define dan-then-dog
  (stream-maker (lambda (x) x)
                (lambda (x)
                  (if (string=? x "dan.jpg")
                      "dog.jpg"
                      "dan.jpg"))
                "dan.jpg"))


(define (stream-add-zero s)
  (stream-maker (lambda (x) (cons 0 (car (x))))
                (lambda (x) (cdr (x)))
                s))


(define (cycle-lists xs ys)
  (letrec ([fn (lambda (i j)
                 (cons (cons (ith xs i) (ith ys j))
                       (lambda ()
                         (fn (remainder (+ i 1) (length xs)) (remainder (+ j 1) (length ys))))))]) 
    (lambda () (fn 0 0))))


(define (vector-assoc v vec)
  (letrec ([check (lambda (i) (cond [(= i (vector-length vec)) #f]
                                    [(pair? (vector-ref vec i)) (let ([p (vector-ref vec i)])
                                                                  (cond [(equal? (car p) v) p]
                                                                        [#t (check (+ i 1))]))]
                                    [#t (check (+ i 1))]))])
    (check 0)))


(define (cached-assoc xs n)
  (letrec ([vec (make-vector n #f)]
           [pos 0])
    (lambda (v) (let ([cache (vector-assoc v vec)])
                  (cond [(equal? #f cache) (begin
                                             ;(println "not found in cache")
                                             (let ([ans (assoc v xs)])
                                               (cond [(equal? ans #f) #f]
                                                     [#t (begin (vector-set! vec pos ans)
                                                                (set! pos (if (= pos (- n 1))
                                                                              0
                                                                              (+ pos 1)))
                                                                ;(println pos)
                                                                ans)])))]
                        [#t (begin
                              ;(println "found in cache")
                              cache)])))))


(define-syntax while-less
  (syntax-rules (do)
    [(while-less e1 do e2)
     (letrec ([x e1]
           [iter (lambda (y) (if (< y x)
                                   (iter e2)
                                   #t))])
       (iter e2))]))

5 1 投票
评分
2条留言
订阅评论
提醒
guest
您想以什么身份发表评论
邮箱将在您的评论被回复时给您通知
(可选)如果您也有个人网站,不妨分享一下
我对这篇文章的评分
这篇文章给您带来多大帮助
在点击发表评论按钮时,网络请求的数据包含浏览器版本、操作系统版本和 IP 地址;您的网络服务提供商、雇主或学校、政府机构可能会看到您的访问活动;根据浏览器默认行为、操作系统设置和安全防护软件的设置不同,您的浏览器可能会也可能不会在本地 Cookies 缓存您输入的用户名、邮箱以便下次评论使用。
2 评论
内联反馈
查看所有评论
kingno
kingno
游客
2021年1月2日 10:37
我对这篇文章的评分 :
     

sicp吗