Erro of sintaxe in the creation of an array

Please, I don’t understand where is the erro in the code below.


quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (s:xs) = quicksort [x|x <- xs,x < s] ++ [s] ++ quicksort [x|x <- xs,x >= s]
        
main :: IO ()
main =  do
   
xs::Array Int Int xs = array (1,9) [(1,9),(1,1),(2,8),(3,2),(4,5),(6,7),(7,3),(8,6),(9,4)]

There are a couple of issues with main and the line below it.

Haskell is whitespace sensitive when using do notation, writing

main = do
    putStrLn "hi"

is different from

main = do
putStrLn "hi"

Furthermore, to assign a value to a name in a do block you should use let:

main = do
    let x = 42
    print x

And then, to type the value of a name, you do

main = do
    let x = 42 :: Int

If you want to have the type next to the name, you need to add {-# LANGUAGE ScopedTypeVariables #-} to the top of the module:

main = do
    let x :: Int = 42

I don’t understand what to do in the case of arrays.
How would my code look?

It’s showing the same error all the time…

[1 of 1] Compiling Main ( jdoodle.hs, jdoodle.o )

jdoodle.hs:10:9: error:
parse error on input ‘=’
Perhaps you need a ‘let’ in a ‘do’ block?
e.g. ‘let x = 5’ instead of ‘x = 5’
|
10 | xs = array (1,9) [(1,9),(1,1),(2,8),(3,2),(4,5),(6,7),(7,3),(8,6),(9,4)]
| ^

GHC is being as helpful as possible here.

@romes has already patiently explained

You’ve declared main :: IO () – good, that’s a typical signature for main. You’ve started main = do – also good. But you’ve not told what to do. Even supposing xs = ... would make sense in a do block (which it doesn’t), what happens next with xs? Do you want to quicksort it? Do you want to print the result?

I don’t see anything specific to array here. Start with baby steps. Try first binding an Int literal to some variable x and getting GHC to print that.

Are you sure with this? Perhaps it could have at least provided helpful link to discover what is possibly wrong.
E.g. what error message would your favorite language give?

Yes. “what is possibly wrong” might be:

  • @Ewerton is not working from any sort of tutorial. (I don’t know of anywhere they might have found code like this.)
  • Or they think Haskell is (C? Java? Javascript? Python? something procedural?).

I don’t expect GHC to guess which tutorial somebody might be using; I don’t expect GHC to guess which other language this code might resemble. I don’t expect GHC to provide a link to a basic Haskell tutorial on do blocks (because there are so many – amongst many other reasons).

I think GHC is making as helpful as possible a suggestion, given the code couldn’t possibly be any sort of Haskell: it’s not like you could switch on an extension and the code would make more sense.

My favourite compiler for my favourite language says:

Syntax error in input (unexpected `=')  -- pointing at the `=` of the binding to `xs`

That’s less helpful than GHC’s message.

My favourite language compiler would’ve given that error verbatim :stuck_out_tongue:

1 Like