I'd like to be able to manipulate sentences so that I can take them as an input and return an output based on things like the individual letters. For example, an ends-e command that would return all of the words that end in "e":
(ends-e '(only the good die young))
=> '(the die)
Unfortunately, "e" is a string, and '(only the good die young) is a sentence. Scheme has trouble understanding a sentence as a data type (because it isn't one). How do I turn a sentence that the user inputs with a quote and parentheses into something I can manipulate and return in the same sentence format?
This book: https://people.eecs.berkeley.edu/~bh/ssch8/higher.html#ft1 outlines some functions that can manipulate sentences and words, but posts a footnote at the bottom saying "Like all the procedures in this book that deal with words and sentences... [the] procedures in this chapter are part of our extensions to Scheme."
How do I get those extensions? I looked in a later chapter, but my understanding of the language is too rudimentary to understand how to create those procedures myself.
These are the error messages I get when I try to turn '(h) into a datatype scheme can understand.
Welcome to DrRacket, version 6.12 [3m].
Language: sicp, with debugging; memory limit: 128 MB.
> (symbol->string '(h))
. . symbol->string: contract violation
expected: symbol?
given: (mcons 'h '())
> (list->string '(h))
. . list->string: contract violation
expected: (listof char?)
given: '(h)
> (string->list '(h))
. . string->list: contract violation
expected: string?
given: (mcons 'h '())
> (string->symbol '(h))
. . string->symbol: contract violation
expected: string?
given: (mcons 'h '())
>
This means I cant ask scheme if '(h) is equal to "h". I can't even ask it if '(h) is equal to '(h)!
> (eq? '(h) "h")
#f
> (eq? '(h) '(h))
#f
>
In DrRacket there is a Simply Scheme compatibility language
Open the Package Manager: in DrRacket choose the menu "File" then choose "Package Manager...".
In the tab "Do What I Mean" find the text field and enter: "simply-scheme" without the quotes.
Click the "Install" button. This produces smoe output. When you can click "close output" it's finished and you may close the window.
Test it. Make sure DrRacket has "Determine language from source" in the bottom left corner. Write the following program and click RUN:
#lang simply-scheme
(se (butlast (bf "this"))
"world")
; ==> (hi "world")
I was a little confused since SICP and Simply Scheme are two different books. SICP has their own specified procedures from their book and in DrRacket there is a specific language, #lang sicp
, for that flavor of Scheme too. I've written a similar answer on how to install sicp.