anubhav
anubhav

Reputation: 131

searching an element in list in ocaml

(* function for union of two ordered sets*)
let rec search n list1 = match list1 with
    [] -> false
    | head :: tail when head = n -> true
    | head :: tail when head != n -> search n tail
;;

(* function calls and output *)
Printf.printf("\nList = {");;
open Printf
let list1=[3;1;2;4];; (* Input first set here *)
let () = List.iter (printf " %d ") list1;;
printf("}");;

n=2;;
let u = search n list1;;

I am getting an error:

File "search.ml", line 15, characters 0-1:
Error: Unbound value n

Line 15 => "n=2;;"

Kindly inform whether it is a syntactic error or otherwise and possible remedy as well. Implementation is done on linux.

Upvotes: 0

Views: 788

Answers (2)

ADEpt
ADEpt

Reputation: 5542

Expression n=2 compares n to 2, however n is not defined yet, so you get an error. You should use let n = 2 to bind values to names.

Upvotes: 1

ivg
ivg

Reputation: 35210

in OCaml to bound a value with a name one should use len name = value syntax

Upvotes: 1

Related Questions