Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell simple compilation bug

Tags:

haskell

I'm trying to run this code:

let coins = [50, 25, 10, 5, 2,1]

let candidate = 11

calculate :: [Int]
calculate = [ calculate (x+candidate) | x <- coins, x > candidate]

I've read some tutorials, and it worked out ok. I'm trying to solve some small problems to give-me a feel of the language. But I'm stuck at this.

test.hs:3:0: parse error (possibly incorrect indentation)

Can anyone tell me why? I've started with haskell today so please go easy on the explanations.

I've tried to run it like:

runghc test.hs
ghc test.hs

but with:

ghci < test.hs

it gives this one:

<interactive>:1:10: parse error on input `='

Thanks

like image 878
fmsf Avatar asked Jan 19 '26 08:01

fmsf


1 Answers

1) Top level declarations don't need 'let'. You probably want:

coins = [50, 25, 10, 5, 2,1]

candidate = 11

2) Calculate is explicitly typed as a list and used as a function.

Here is where you say calculate is a list of integers:

calculate :: [Int]

And inside the list comprehension you used calculate (x+candidate), but you already explicitly made calculate a list and not a function - so this can not work.

calculate = [ calculate (x+candidate) | x <- coins, x > candidate]

Perhaps you wanted something like:

newCoins = [ x + candidate | x <- coins, x > candidate]

It would help if you explained more of what you want as a result.

like image 63
Thomas M. DuBuisson Avatar answered Jan 21 '26 01:01

Thomas M. DuBuisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!