2016-11-04 6 views
-1

私はこの練習問題リストから[a1、...、ai、ai + 1、...、an]をリストする[ai + 1、...、an、a1、.... ai]

私の考えは、2つの関数を作成することである。

  1. 最初の機能は、リスト[ai + 1, ..., an]
  2. 第2の機能を作成し、入力として最初の関数の結果を取得し、結果を返し:[ai + 1, ..., an, a1, ..., ai]

問題は、私がOcamlでプログラミングを始めたばかりであり、使用方法がわかりません。だから私は解決する方法を知らないいくつかのエラーがあります。 私のコードはこれです:

let rec produceprima l i = 
let rec produceprima_aux l i acc= 
    let rec aux l i acc l1 = 
    match l with 
    []-> [] 
    |x::y -> if(acc>i) then aux y i acc+1 [email protected][x] 
      else aux y i acc+1 l1 
    in aux l i acc l1 
in produceprima_aux l i acc;; 

let rec produceseconda l i = 
let rec produceseconda_aux l i acc= 
    let rec aux l i acc l1 = 
    match l with 
    []-> [] 
    |x::y -> if(acc<=i) then aux y i acc+1 [email protected][x] 
    in aux l i acc l1 
in produceseconda_aux l i acc;; 

最初の関数の誤差がある:

Error: This expression has type 'a -> 'b list 
     but an expression was expected of type int 

第二の機能は、私が試してみ持っていますが、確かに、それはエラーを持っています!

答えて

0

f x+1は、(f x) + 1としてコンパイラによって解析されます。オペレータ名の周りの空白を省略したという事実は、それを優先しませんでした。したがって、aux y i acc+1 l1の代わりにaux y i (acc+1) l1と書く必要があります。

これは非常によくある間違いです。 OCamlProチュートリアルを試してみることをお勧めします。特に、構文トラップに焦点を当てたレッスン5。

+0

ありがとうございました:私はそれを試してみてください!確かにチュートリアルが表示されます! –