7 Replies - 5066 Views - Last Post: 06 July 2013 - 02:31 PM

#1 jayy_two   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 28-October 12

sort list of numbers in haskell

Posted 31 March 2013 - 10:19 AM

i am new to haskell. I need to sort a list of numbers. so far this is what i have come up with.
import Data.List

 numbers = [6,2,1,7,9]
 num = List.sort numbers



but it gives me the error
prog.hs:3:2: parse error on input `numbers'



how do i fix this?
Is This A Good Question/Topic? 0
  • +

Replies To: sort list of numbers in haskell

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: sort list of numbers in haskell

Posted 31 March 2013 - 10:23 AM

Haskell is a language with significant indentation. Don't indent code that isn't part of a previous block, expression or definition. In the code you've posted here nothing should be indented.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: sort list of numbers in haskell

Posted 31 March 2013 - 10:46 AM

Another problem with your code is that you refer to Data.List.sort as List.sort. You need to refer to it as either Data.List.sort or just sort. If you want to refer to it as List.sort, your import statement would need to be import Data.List as List or import qualified Data.List as List (the latter would make it illegal to refer to the functions defined in Data.List without prefixing them with List. while the former would allow both List.sort and sort).
Was This Post Helpful? 1
  • +
  • -

#4 jayy_two   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 28-October 12

Re: sort list of numbers in haskell

Posted 31 March 2013 - 11:43 AM

I fixed all that
module Main where

import Data.List

numbers = [6,2,1,7,9]
num = Data.List.sort numbers


and now it says
 prog.hs:1:1: The function `main' is not defined in module `Main'

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: sort list of numbers in haskell

Posted 31 March 2013 - 11:56 AM

There are three things you can do with a Haskell file (assuming you're using GHC):

  • Open it in the interpreter. You do this by writing ghci myfile.hs. Once you've done this you can examine the values of the variables defined in your file, call the functions defined in your file and evaluate arbitrary Haskell expressions using the variables and functions defined in your file.
  • Compile your file as a module that can be used by other modules. For this your file needs to start with module FooBar where where FooBar can be any identifier that starts with a capital letter and isn't Main, but the file name needs to be equal to the module name. You can then compile the module using ghc FooBar.hs and import it from other Haskell files that can then use the functions and variables defined in your module.
  • Compile your file as an executable. For this the file needs to either start with module Main where or not start with a module definition at all. It also needs to define main with the type IO (). main will then be run when your executable is invoked. You can compile it using ghc myfile.hs.


In your case you called your module Main, but did not define main. Therefore the only thing you can do with your file is to load it in the interpreter. If you want to compile it into an executable, you'll need to define main. If you want to use it as a module from other files, you'll have to name your module something other than Main.
Was This Post Helpful? 1
  • +
  • -

#6 patons02   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 14
  • Joined: 05-February 09

Re: sort list of numbers in haskell

Posted 05 July 2013 - 08:25 AM

Possibly a bit late to reply to this but it will stand as an excellent reference point. Firstly I will show you the mistakes in that code.

As sepp2k pointed out, you must define a main function if your module is called Main. In Haskell, the GHCi interactive interpreter will allow .hs and .lhs files to be accepted without a Main module, and you can just run your function which in this case is num.

Fixing the code it looks like this:
module Main where

import Data.List

numbers :: [a]
numbers = [6,2,1,7,9] --List of numbers 

num :: [a]
num = sort numbers -- Sort numbers using the sort function from Data.List

main = do
  putStrLn num -- Allow the num function to be ran when the program executes.




From this we can define other sorting functions to use in place of sort. Here is quicksort.

qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort l ++ [x] ++ qsort r
  where
  l = filter (< x) xs
  g = filter (>= x) xs


Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: sort list of numbers in haskell

Posted 05 July 2013 - 08:40 AM

numbers :: [a]
numbers = [6,2,1,7,9] --List of numbers 



That's a type error. The type should be Num a => [a] or something more specific if you want.

num :: [a]
num = sort numbers -- Sort numbers using the sort function from Data.List



Same as above.

main = do
  putStrLn num -- Allow the num function to be ran when the program executes.



That should be print num - putStrLn expects a string as its argument and num is not a string. print however accepts any type that's an instance of Show.

Also num is not a function, it's a list.
Was This Post Helpful? 1
  • +
  • -

#8 patons02   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 14
  • Joined: 05-February 09

Re: sort list of numbers in haskell

Posted 06 July 2013 - 02:31 PM

Ahh yes! This is what happens when you forget Haskell for a month and then try to code off the top of your head without GHCi !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1