0

I try using foldr to reverse a list like the following

fun rev(l) = foldr (a b => b ++ [a]) [] l;

but I got error : stdIn:4.25 Error: syntax error found at DARROW

Can anyone help to point out the mistake? Is the code correct?

Thank you.

1 Answers1

0
  1. The DARROW error is thrown because a lambda expression should start with a fn.
  2. Foldr gets a tuple, so the lambda expression should begin with fn (a,b)
  3. ++ is the concat operator in Haskell. In SML it's @

Which all sum up to:

fun rev(l) = foldr (fn (a,b) => b @ [a]) [] l;

And this indeed reverses a list!

Benesh
  • 3,398
  • 1
  • 18
  • 38