0

I'm trying to make shifting of array. But I'm getting syntax error on arr.length. (2nd line)

let shift (arr : array) (k : int) =
    let size = arr.length;;
    let shifted = Array.make size 0;;

    for i = 0 to size- 1 do
        if i < k
        then shifted.(size- 1 - k + i) <- arr.(i);;
        else shifted.(i-k) <- arr.(i);;
    done

    shifted;;

2 Answers 2

5

There are a few problems with your code.

First, the ;; symbol really isn't part of OCaml syntax. It's used only in the interpreter (the toplevel or REPL) to tell the interpreter when to evaluate what you've typed so far. In ordinary source code it's only rarely used. Many people (including me) adopt a style where ;; is never used in source files.

By contrast, you are ending just about every line with ;;, which is guaranteed to cause syntax errors.

The way to define local variables of a function looks like this:

let var = expr in
(* rest of function comes here *)

So you should have

let size = Array.length arr in
let shifted = Array.make size 0 in
. . .

You should remove all your ;; tokens and add in as necessary.

In addition, you are using a kind of object-oriented syntax. But Arrays are not OO-style objects. To get the length of an array named arr:

Array.length arr

Here's how it looks:

# let arr = Array.make 10 0;;
val arr : int array = [|0; 0; 0; 0; 0; 0; 0; 0; 0; 0|]
# Array.length arr;;
- : int = 10
# Array.length [| "another"; "example" |];;
- : int = 2

In other words, length is a function defined in the Array module. An OCaml array isn't an object (of the OO sort) and doesn't have methods.

(For future reference, methods of OCaml objects are referenced using #, as in object#method.)

0
0

As an added suggestion, we can use Array.init and exception handling to implement this quite readily by having the Invalid_argument exception thrown by an out of bounds access handled by returning 0.

# let shift arr k =
    Array.init 
      (Array.length arr)
      (fun i -> try arr.(i + k) with Invalid_argument _ -> 0)
val shift : int array -> int -> int array = <fun>
# shift [|1; 2; 3; 4|] 1;;
- : int array = [|2; 3; 4; 0|]
# shift [|1; 2; 3; 4|] 2;;
- : int array = [|3; 4; 0; 0|]

A further refinement might provide a default value so that the type of shift can be generalized to 'a array -> int -> 'a -> 'a array.

Or if the default is a function, it would let us generate unique values. Since arrays are mutable, if the type of the default is mutable, this may be beneficial.

let shift_generalized arr k default =
  Array.init
    (Array.length arr)
    (fun i -> 
        try arr.(i + k) 
        with Invalid_argument _ -> default i)

Our original shift now might be implemented as follows.

let shift arr k =
  shift_generalized arr k (Fun.const 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.