如何更改球拍中列表内容的顺序? [英] How to change the order of the contents of a list in racket?

查看:56
本文介绍了如何更改球拍中列表内容的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是球拍的初学者,正在学习列表的工作方式.我必须创建一个函数,该函数接受一个count变量和一个列表,并生成相同的列表,但第一个元素循环到列表"count"的末尾次数.我的代码适用于偶数列表,例如调用函数2'(1 2 3 4),这将导致列表'(3 4 1 2),但不适用于奇数列表,例如调用2'(1 23),其结果应为'(3 1 2)

I'm a beginner in racket and am learning how lists work. I have to create a function that takes in a count variable and a list and produces the same list but with the first element cycled to the end of the list "count" number of times. My code works for even numbered lists, like calling the function 2 '(1 2 3 4), which results in a list '(3 4 1 2), but does not work for odd numbered lists, like calling 2 '(1 2 3), which should result in '(3 1 2)

(define (cycleN count aList)
  (cond
    [(empty? aList) '()]
    [(= count 0) aList]
    [(= count (length aList)) aList]
    [else (append (take-right aList count) (take aList count))]))

我想念什么?

推荐答案

您应该正确使用 -ing (length aList)-count 个元素,而不是计数.

You should be take-right-ing (length aList) - count elements, not count.

这篇关于如何更改球拍中列表内容的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆