I'm new to Haskell, and I like this page. So, wanting to get a feel for it, I tried typing the sieve example from the top of the page into the "Type Haskell expressions in here." box right beneath it.
First, I got
<hint>:1:8: parse error on input `='
Hm so apparently definitions aren't expressions? Or something?
I decided to remove the `primes = ` part, which made my browser really slow and had no result. I'm assuming it was computing all primes in the background.
Now, of course, maybe I should try to understand the sieve example before running it, but isn't the whole point of having a REPL box on the front page that you can facilitate people who learn the other way around? It would be great if there'd be some code or expressions available at my fingertips that would actually work in that REPL.
In other words, maybe make the first example be something that actually terminates?
Please read this as constructive criticism, which is how it is intended.
EDIT: Only now I'm noticing that exactly the things I ask for are listed to the right of the "try" box. Pardon my lazy reading, I'm really enjoying the tutorial right now. Still, making the `sieve` example terminate would be nice. But I believe my nitpick is maybe a bit much of a nitpick now.
1. multi-line expressions aren't supported by the REPL.
2. You will need to use `let x = ..` syntax in the REPL (This is because the REPL are just so called IO-actions and the `let` syntax defines variables within its lexical scope)
You could try this:
> let sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
> let primes = sieve [2..]
> primes !! 3
> primes !! 4
Nice part abou this code is eventhough you express calculating ALL primes. It will only calculate upto the 3rd and the 4th prime due to lazy evaluation. This is one of the great strengths of haskell.
The biggest problem isn't what others have said. The biggest problem is that the try box doesn't let you define your own functions or variables.
Therefore, even if you were an old hand at GHC and GHCi and so on, you'd still be unable to run the code it shows at the top of the page.
This is probably bad website design. Especially since the website still lets randoms burn themselves by entering non-terminating code that makes their browsers slow.
> I agree with the problems but am curious how you would solve the non-terminating code problem?
I thought it was running on the browser, based on what the OP said, so that would entail opcode-counting or, if you can use multiple threads, watchdog timers. That is, don't try to solve the halting problem, just enforce an arbitrary limit to how long code can run.
However, if the code is being run on the server, you could enforce CPU and RAM quotas to do the same thing with the OS's help.
Yeah, the online REPL is definitely not very useful or intuitive yet.
As it says, you can evaluate a single line Haskell "expression" and top-level declarations (like the sieve example) are not expressions. In addition, the sieve example declares an infinite list so there's no sensible way to print it.
You can fix all the above by using a let-expression on single line and only evaluating a finite part of the list using the 'take' function.
let { primes = sieve [2..]; sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0] } in take 10 primes
Just to mention it here, IHaskell solves a lot of the annoyances with ghci. Easy multi-line input, don't need let in front of functions, can define modules and load them (automatically compiled), etc. I think they are working on support for template haskell too (important for experimenting with lens code etc)
First, I got
Hm so apparently definitions aren't expressions? Or something?I decided to remove the `primes = ` part, which made my browser really slow and had no result. I'm assuming it was computing all primes in the background.
Now, of course, maybe I should try to understand the sieve example before running it, but isn't the whole point of having a REPL box on the front page that you can facilitate people who learn the other way around? It would be great if there'd be some code or expressions available at my fingertips that would actually work in that REPL.
In other words, maybe make the first example be something that actually terminates?
Please read this as constructive criticism, which is how it is intended.
EDIT: Only now I'm noticing that exactly the things I ask for are listed to the right of the "try" box. Pardon my lazy reading, I'm really enjoying the tutorial right now. Still, making the `sieve` example terminate would be nice. But I believe my nitpick is maybe a bit much of a nitpick now.