Search code examples
haskellfunctional-programmingsmlcategory-theorycatamorphism

Having trouble translating code from Haskell to SML


I am trying to translate the following piece of code from SML to haskell but I'm having a bit of trouble.

type List_alg x u = (u, x->u->u)
list_cata :: List_alg x u -> [x] -> u list_cata (a,f) = cata where
  cata[] =a
  cata (x:l) = f x (cata l)

Here is what I've tried:

type ('a, 'b) List_alg = 'b * ('a -> 'b -> 'b)

fun list_cata (((a, f): List_alg), (l: 'a list)): 'b = 
  case l of
       [] => a
     | x::xs => f x (list_cata ((a,f), xs))

I get the error Error: type constructor List_alg given 0 arguments, wants 2 but I'm not sure what is wrong/how to fix that. Any help would be appreciated!


Solution

  • List_alg is a type constructor that expects two arguments, but you used it without any arguments on the third line. Try changing it to ('a, 'b) List_alg as in

    fun list_cata (((a, f): ('a, 'b) List_alg), (l: 'a list)): 'b = 
    

    Notice that your Haskell implementation also passes x and u after List_alg