1

I am learning 61a from UCB using MITScheme.

For the first few chapters, I have been using windows (First download cygwin, then run XWin type emacs & and I get my emacs editor and REPL).

This has worked fine but I have switched to Ubuntu and I am using MIT Scheme. It has been working well but I recently ran into a problem.

[I am using Release 9.1 of MIT/GNU Scheme on Ubuntu and STk Interperter version 4.0.1 on Windows]

I am trying to write a black jack program.

I take the input as a hand of cards '(ad 8s 5h) [for ace diamond, 8 spades, 5 hearts].

To access the 'a value, the UCB lectures and the windows version are fine with:

(first (first '(ad 8s 5h))) ===> a

When I try this on Ubuntu MIT Scheme, I get the error:

The object ad, passed as an argument to safe-car, is not a pair.

After some testing I see that MIT scheme here will use first to extract the first item of a list:

(first '(ad 8s 5h)) =====> ad

But it will not use first as a selector for the first letter of a single word.

Can anybody give me a suggestion? I would like to keep learning this course on Ubuntu because it makes my life much easier in terms of saving and opening files for example.

Thanks,

AJ

4

1 回答 1

0

用于first访问符号中的第一个字符不能跨 Scheme 实现移植。在大多数口译员中firstcar适用于成对。要访问符号中的第一个字符,请创建一个帮助程序:

(define (first-symbol sym)
  (string->symbol               ; 4. convert the string to a symbol
   (string                      ; 3. create a one-character string
    (string-ref                 ; 2. get the first character in the string
     (symbol->string sym) 0)))) ; 1. convert the symbol to a string

上述函数仅使用标准程序,它应该在任何 RxRS 解释器中都能正常工作。像这样使用它:

(first-symbol (first '(ad 8s 5h)))
=> 'a
于 2013-07-23T18:38:51.400 回答