P18 (**) Extract a slice from a list.
来源:互联网 发布:win10磁盘优化闪退 编辑:程序博客网 时间:2024/04/29 16:42
问题描述
Given two indices, I and K, the slice is the list containing the elements between the I’th and K’th element of the original list (both limits included). Start counting the elements with 1.
sash> (slice '(a b c d e f g h i k) 3 7)sash> (c d e f g)
解法
递归实现
(define slice (lambda (ls I K) (let f ([i 1] [s ls]) (cond [(> i K) '()] [(<= I i K) (cons (car s) (f (+ i 1) (cdr s)))] [else (f (+ i 1) (cdr s))]))))
基本思路是:
(1) 索引I之前的元素丢弃;
(2) [I, K]之内的元素累积;
(3) 索引K+1作为递归结束条件。
切片操作是比较常见的操作,可惜r7rs中的list-copy
只能返回整个列表的副本,没有提供其他重载。但string-copy
提供了多个重载。这样我们可以:
(1) 将list转化为string:list->string;
(2) string-copy得到新的字符串;
(3) string->list得到列表。
间接实现题目。
1 0
- P18 (**) Extract a slice from a list
- P18 (**) Extract a slice from a list.
- P23 (**) Extract a given number of randomly selected elements from a list.
- Extract channel information from a video
- How to extract .obj from a .lib
- jar - user of jar command to list/extract file from a jar file
- extract the x509 ssl certificate from a pcap file
- A value is trying to be set on a copy of a slice from a DataFrame
- remove a loop from a linked list
- codeforces A.Extract Numbers
- decruft(A library to extract meaningful data from a webpage) 源码分析
- 问题解决:SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
- Extracting Unique Elements From a List
- C++: delete a node from linked list
- Extract raw SOAP Message returned from Foreign Web Service after Deserialization Failure in a Web Service
- My excersize program used to extract English words from a library
- Extract the shortest summary contains all the keywords from a article
- Extract lowercase masked FASTA from a BLAST database with masking information
- 使用docker搭建部署hadoop分布式集群
- Linux下Gcc生成和使用静态库和动态库详解(转)
- 在mac上安装mysql5.7.10
- P1025 数的划分
- 在Linux下如何使用GCC编译程序、简单生成静态库及动态库。
- P18 (**) Extract a slice from a list.
- Apache常见编译选项-转
- 关于sqlite断电或者存储空间不足造成的数据文件损坏问题-转
- php 密码加密方式
- makefile how to
- Android IPC 之AIDL
- 安装cocoapods
- Bash中命令连接符的用法——一次执行多个命令-转
- Ubuntu 12.04中设置安装Google拼音输入法