如何从非空字符串(DrRacket)中提取最后一个字符。希望得到帮助 [英] How do you extract the last character from a non-empty string (DrRacket). Hope for assistance

查看:205
本文介绍了如何从非空字符串(DrRacket)中提取最后一个字符。希望得到帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Scheme很新,我正在使用DrRacket,我希望得到一些急需的帮助。



我的第一个任务是提取第一个来自非空字符串的字符,我成功地这样做了。



;;签名:字符串优先:字符串 - > string

;;目的:从非空字符串中提取第一个字符

;;测试

(check-expect(string-第一个cookies)c)

(check-expect(字符串优先聚会)p)

;;定义:string-first

(define(string-first str)

(string-ith str 0))



现在,我是负责从非空字符串中提取最后一个字符。我无法弄清楚这一点。我的猜测是它涉及字符串长度与string-ith相结合。



希望有人可以帮助我。提前谢谢。

解决方案

阅读文档有什么问题?请参阅:

http:/ /sicp.ai.mit.edu/Fall-2004/manuals/scheme-7.5.5/doc/scheme_7.html [ ^ ]。



找到过程:过程:string-ref string k 。指数从零开始,因此您需要索引 length - 1 。另请参阅过程 string-length string



-SA

I'm fairly new to Scheme and I'm using DrRacket and I hope to get some much needed assistance.

I was first tasked to extract the first character from a non-empty string, which I was successful in doing so.

;;Signature: string-first: string -> string
;;Purpose: To extract the first character from a non-empty string
;;Tests
(check-expect (string-first "cookies") "c")
(check-expect (string-first "party") "p")
;;Definition: string-first
(define (string-first str)
(string-ith str 0))

Now, I am tasked with extracting the last character from a non-empty string. I cannot figure this out. My guess is that it involves "string-length" in combination with "string-ith."

Hopefully someone can help me out. Thank you in advance.

解决方案

What's wrong with just reading the documentation? Please see:
http://sicp.ai.mit.edu/Fall-2004/manuals/scheme-7.5.5/doc/scheme_7.html[^].

Locate the procedure: "procedure: string-ref string k". Indices are zero-based, so you need the index length − 1. Also see the procedure "string-length string".

—SA


这篇关于如何从非空字符串(DrRacket)中提取最后一个字符。希望得到帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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