I have started studying Haskell, and I am try to write a "reverse" function. I created 2 functions for that, and they work, but now I want to define one locally within the second, but I get syntax errors. Here's what I'm doing:

let liste1 = [4,2,8,1,7,12]  
-- I create a random list

test(x,y)= if x==[] then y else test(tail(x),head(x):y) 
-- take a 2 list, with y=[] to have a list memory

inverse(x)=test(x,[]) 
-- and finally, to have only 1 parameter

and then, we have:

> inverse(liste1)
[12,7,1,8,2,4]

It's works! Amazing!

Now I want to have test as a LOCAL function. This is what I have tried so far:

inverse(x) = let test(x,[]) in if x==[] then y else test(tail(x),head(x):y)

and

inverse(x) = let  if x==[] then y else test(tail(x),head(x):y) in test(x,[])

Both ways don't work, and I get the following error: parse error on input ├Čn'

I know it's basic question, and more basic solution, but I don't get it... Help me please!

1 Answers

2
rampion On Best Solutions

When adding definitions using let, you want to put your definition between let and in.

inverse(x) =
  let test(x,y) = if x==[] then y else test(tail(x),head(x):y) 
  in  test(x,[])

You could instead use where to give the definition afterwards:

inverse(x) = test(x,[])
  where test(x,y) = if x==[] then y else test(tail(x),head(x):y)