OCaml check whether variable is string

how can I check whether variable is string or not? Is there any function in OCaml which takes one variable as argument and returns true if it’s string and false if not?

Do you mean:

let tautology (x:string) = true

More seriously, it doesn’t really make sense to test the type of a variable in OCaml.

The OCaml typechecker does that for you at compile time. There’s no need to do it at runtime. Maybe you can explain your use case though.

I want read file line by line and save this lines into the list but I need base case for recursion.
example of pseudocode:
let rec savestrinlist file = if (there must be condition to check if (input_line file) is not string or char) then [] else (input_line file)::savestringinlist file;;

It might be easier to read the entire file into a string and split the string on newlines into a list of strings. Unless, of course, you don’t want to read the entire file. The size of a file is in_channel_length ic and really_input_string ic reads it from an input channel ic. If you read the file line by line, you need to watch for the end of the file as the base case for the recursion. This is typically signalled by an exception.

1 Like

input_line in_channel always gives a string, by definition. So any condition for checking that would always return true. The base case for recursion is to handle the End_of_file exception that may be thrown by input_line. When you hit the end of the file, you know you won’t recurse any more.

yeah, I did same… thank you