CS61A Homework 6

本作业指导学生通过实现多个Scheme函数来加深对列表操作的理解。包括提取列表中的特定元素、检查列表是否递增排序、交错合并两个列表、过滤列表中符合特定条件的元素以及去除列表中的重复项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更好的阅读体验

Homework 6: Scheme, Scheme Lists hw06.zip

Due by 11:59pm on Thursday, April 7

Instructions

Download hw06.zip. Inside the archive, you will find a file called hw06.scm, along with a copy of the ok autograder.

Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy.org. See Lab 0 for more instructions on submitting assignments.

Using Ok: If you have any questions about using Ok, please refer to this guide.

Readings: You might find the following references useful:

Grading: Homework is graded based on correctness. Each incorrect problem will decrease the total score by one point. There is a homework recovery policy as stated in the syllabus. This homework is out of 2 points.

Required Questions

Getting Started Videos

Code Writing Questions

Q1: Thane of Cadr

Define the procedures cadr and caddr, which return the second and third elements of a list, respectively. If you would like a quick refresher on scheme syntax consider looking at Lab 10 Scheme Refresher.

(define (cddr s)
  (cdr (cdr s)))

(define (cadr s)
    'YOUR-CODE-HERE
    (car (cdr s))
)

(define (caddr s) 
    'YOUR-CODE-HERE
    (car (cdr (cdr s)))
)

Use Ok to unlock and test your code:

python3 ok -q cadr-caddr -u
python3 ok -q cadr-caddr✂️

Q2: Ascending

Implement a procedure called ascending?, which takes a list of numbers lst and returns True if the numbers are in nondescending order, and False otherwise. Numbers are considered nondescending if each subsequent number is either larger or equal to the previous, that is:

1 2 3 3 4

Is nondescending, but:

1 2 3 3 2

Is not.

Hint: The built-in null? function returns whether its argument is nil.


(define (ascending? lst) 
    'YOUR-CODE-HERE
    (if (null? lst)
        #t
        (if (null? (cdr lst))
            #t
            (if (<= 
                    (car lst) 
                    (car (cdr lst))
                )
                (ascending? (cdr lst))
                #f
            )
        )
    )
)

Use Ok to unlock and test your code:

python3 ok -q ascending -u
python3 ok -q ascending✂️

Q3: Interleave

Implement the function interleave, which takes a two lists lst1 and lst2 as arguments. interleave should return a new list that interleaves the elements of the two lists. (In other words, the resulting list should contain elements alternating between lst1 and lst2.)

If one of the input lists to interleave is shorter than the other, then interleave should alternate elements from both lists until one list has no more elements, and then the remaining elements from the longer list should be added to the end of the new list.

(define (interleave lst1 lst2)
    'YOUR-CODE-HERE
    (if (null? lst1)
        lst2
        (cons (car lst1)
            (interleave lst2 (cdr lst1))
        )
    )
)

Use Ok to unlock and test your code:

python3 ok -q interleave -u
python3 ok -q interleave✂️

Q4: My Filter

Write a procedure my-filter, which takes a predicate func and a list lst, and returns a new list containing only elements of the list that satisfy the predicate. The output should contain the elements in the same order that they appeared in the original list.

Note: Make sure that you are not just calling the built-in filter function in Scheme - we are asking you to re-implement this!

(define (my-filter func lst) 
    'YOUR-CODE-HERE
    (if (null? lst)
        lst
        (if (func (car lst))
            (cons (car lst)
                (my-filter func (cdr lst))
            )
            (my-filter func (cdr lst))
        )
    )
)

Use Ok to unlock and test your code:

python3 ok -q filter -u
python3 ok -q filter✂️

Q5: No Repeats

Implement no-repeats, which takes a list of numbers lst as input and returns a list that has all of the unique elements of lst in the order that they first appear, but no repeats. For example, (no-repeats (list 5 4 5 4 2 2)) evaluates to (5 4 2).

Hint: How can you make the first time you see an element in the input list be the first and only time you see the element in the resulting list you return?

Hint: You may find it helpful to use the my-filter procedure with a helper lambda function to use as a filter. To test if two numbers are equal, use the = procedure. To test if two numbers are not equal, use the not procedure in combination with =.

(define (no-repeats lst)
    'YOUR-CODE-HERE
    (define (member item items)
        (if (null? items)
            #f
            (if (equal? item (car items))
                #t
                (member item (cdr items))
            )
        )
    )
    
    (if (null? lst)
        lst
        (if (member (car lst) (cdr lst))
            (no-repeats (cdr lst))
            (cons (car lst)
                (no-repeats (cdr lst))
            )
        )
    )
)

Use Ok to unlock and test your code:

python3 ok -q no_repeats -u
python3 ok -q no_repeats✂️
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值