2

私は今日 F# の勉強を始めたばかりで、 http: //www.tryfsharp.org/Learn/getting-started#data-structures にある F# チュートリアルに取り組み始めました。

上記のセクションでは、レコードとオプションの種類を説明するために、コードの 3 つのスニペットが提供されています。

type Book =
  { Name: string;
    AuthorName: string;
    Rating: int option;
    ISBN: string }


let unratedEdition = 
   { Name = "Expert F#";
     AuthorName = "Don Syme, Adam Granicz, Antonio Cisternino";
     Rating = None;
     ISBN = "1590598504" }


let printRating book =
match book.Rating with
| Some rating -> 
  printfn "I give this book %d star(s) out of 5!" rating
| None -> printfn "I didn't review this book"

私はそのようにprintRatingを適用できると思った

printRating unratedEdition

しかし、次のエラーが表示されます

stdin(63,13): error FS0001: This expression was expected to have type
    FSI_0005.Book    
but here has type
    FSI_0009.Book 

私はここで何が間違っているのかについてちょっと立ち往生しています。私が完全に行方不明になっている明らかな理由はありますか?

4

2 に答える 2

3

問題の解決方法を理解していただき、チュートリアルを続けていただければ幸いです。

Try F# でのコード スニペットの自動読み込みと評価は少しわかりにくいと思います。Book問題は、 と を定義した最初のスニペットを最初に評価することですunratedEdition。次に、再定義する 2 番目のスニペットを評価します。Book現在、F# インタラクティブでは、これは以前の定義を隠している別の型です。これは、新しいバージョンの でprintRating機能する関数です。電話すると:Book

printRating unratedEdition

これは、古い型の値を引数として新しいprintRating型を取る関数です(以前の対話から定義されているため、新しい型に自動的に更新されず、2 つの型は互換性がありません)。 Book BookunratedEditionBook

これは、次の 3 つのスニペットを 1 つずつ評価すると理解できます。

// Snippet #1: Define first version of the 'Book' type and a value of
// this type named 'unratedEdition'
type Book =
  { Name: string; AuthorName: string; Rating: int option; ISBN: string }

let unratedEdition = 
   { Name = "Expert F#"; Rating = None; ISBN = "1590598504";
     AuthorName = "Don Syme, Adam Granicz, Antonio Cisternino"; }

// Snippet #2: Now, we re-define the 'Book' type (we could also add/remove
// fields to make it actually different, but even without that, this still
// defines a new type hiding the original one). We also define a function that
// operates on the new 'Book' type
type Book =
  { Name: string; AuthorName: string; Rating: int option; ISBN: string }

let printRating book =
  match book.Rating with
  | Some rating -> 
    printfn "I give this book %d star(s) out of 5!" rating
  | None -> printfn "I didn't review this book"

// Snippet #3: This will not work, because we are calling function taking new 
// 'Book' with old 'Book' as an argument. To make this work, you need to evaluate
// one (or the other) definition of Book, then evaluate 'unratedEdition' and then
// 'printRating' (so that the value and function operate on the same 'Book' type)
printRating unratedEdition

上記のコードはBook2 回定義されているため、エディターは無効であると不平を言うことに注意してください。そのため、新しいスニペットを読み込むときにエディターのコンテンツを消去する F# を試してください。

于 2013-05-27T10:55:20.343 に答える
1

上記のコードをすべて一度に実行することで、私自身の問題を解決しました。つまり、3 つのスニペットすべてと私の投稿

printRating unratedEdition

一緒に REPL に入り、RUN を押します。以前は、個々のスニペットごとに「ロードして実行」を使用していました。REPL に何らかの問題があるか、REPL がどのように機能するかについての私の理解が限られているに違いないと思います。

編集**チュートリアル全体で、この問題に何度も遭遇しました。したがって、エラーが発生し、その理由がわからない場合は、関連するすべてのコードを REPL に挿入してみてください。これにより、これまでに遭遇したすべての問題が解決されました。

于 2013-05-27T09:36:44.037 に答える