http://www.muitovar.com/monad/moncow.xhtml#list

contains a cross function which calculates the cross product
of two lists.  That attached does the same but then
used cross on 3 lists.  Naturally, I thought use of
fold could generalize that to n lists; however,
I'm getting error:

{-- cut here
Compilation started at Sun Nov 23 13:15:24

make
runghc -XMultiParamTypeClasses -XFunctionalDependencies -XFlexibleInstances cross.hs

cross.hs:23:30:
    Occurs check: cannot construct the infinite type: a = (a, a)
      Expected type: [a] -> [a] -> [a]
      Inferred type: [a] -> [a] -> [(a, a)]
    In the first argument of `Data.Foldable.foldl1', namely `cross'
    In the first argument of `print', namely
        `(Data.Foldable.foldl1 cross (list2, list3, list1))'
make: *** [run] Error 1

}-- cut here

How do I apply fold to cross and n lists, for n>=0?

TIA.

-regards,
Larry
{-
  Purpose:
    Demonstrate http://www.muitovar.com/monad/moncow.xhtml#list
-}

import Data.Foldable

cross::[a]->[b]->[(a,b)]
cross ls1 ls2 = do
  { x<- ls1
  ; y<- ls2
  ; return (x,y)
  }
list1=[1,2]
list2=[10,20]
list3=[100,200]
main = do
  putStrLn "cross list2 list3="
  print (cross list2 list3)
  putStrLn "cross list1 (cross list2 list3)="
  print (cross list1 (cross list2 list3))
  print (list2,list3,list1)
  print (Data.Foldable.foldl1 cross (list2,list3,list1))

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to