user3075773
user3075773

Reputation: 139

Basic OCaml function returns type error

I've been trying to run this function :

let insert_char s c =
let z = String.create(String.length(s)*2 -1) in 
for i = 0 to String.length(s) - 1 do
    z.[2*i] <- s.[i];
    z.[2*i+1] <- c;
done;
z;;

print_string(insert_char("hello", 'x'));;

However the interpreter returns a type error at the last line "type is string * char" and it expected it to be string. I thought my function insert_char created a string. I don't really understand, thanks.

Upvotes: 0

Views: 62

Answers (2)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66808

You define your function as a curried function, but you're calling it with a pair of values. You should call it like this:

insert_char "hello" 'x'

OCaml doesn't require parentheses for a function call. When two values are placed next to each other with nothing between, this is a function call.

Upvotes: 2

gsg
gsg

Reputation: 9377

The syntax of function application in OCaml is f arg1 arg2, not f(arg1, arg2). So it would be print_string (insert_char "hello" 'x').

(Once you fix that you'll discover that your code has other problems, but that is unrelated to your question.)

Upvotes: 0

Related Questions