mardi 12 février 2019

How to fix: Type Error when moving column in Associative List

I am trying to move a column in an associative list and have code that upon my inspections appears to be valid.

Everything seems to be valid and in order but when I run in utop I get a type error described below

let rec get_src src alist = match alist with
  |[]-> failwith "no source"
  |(r,c,d)::t -> if c = src then (r,c,d) else get_src src t

let move_column (src:int) (dst:int) alist =
  let src_elt = get_src src alist in
  let rec move_helper src dst alist new_list = match alist with
    |[] -> new_list
    |(r,c,data)::tail ->
       if (dst < 1 || src < 1) then failwith ("index out of range")
       else if (src < dst) then
         (if ((c < src) || (c > dst)) then move_helper src dst tail ((r,c,data)::new_list)
          else if (c = src) then move_helper src dst tail new_list
          else if (c = dst) then move_helper src dst tail ((r,c-1,data)::src_elt::new_list)
          else move_helper src dst tail ((r,c-1,data)::new_list))
       else if (src > dst) then
         (if (c < dst) || (c > src) then move_helper src dst tail ((r,c,data)::new_list)
          else if (c = dst) then move_helper src dst tail (src_elt::new_list)
          else move_helper src dst tail ((r,c+1,data)::new_list))
  in move_helper src dst alist [];;

if (c < dst) || (c > src) then move_helper src dst tail ((r,c,data)::new_list)

Error: This expression has type ('a * int * 'b) list but an expression was expected of type unit

I am not sure where I put new_list's type as the unit type. May it have to do with my get_src function?

Aucun commentaire:

Enregistrer un commentaire