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.`let x = 4` is not an expression there.
let primes = sieve [2..] where sieve (p:xs) = p : sieve [x| x <- xs, x `mod` p /= 0] in primes
Which is rather annoying. let primes = ... in take 5 primes
... if you want the computation to complete without hanging the browser. It would be nice if the REPL supported multi-line expressions.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 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.
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 primesMy best guess is that it's because it's implemented as an HTTP service that happens not to retain the ghci process between expressions.
If you want to get know more about it: http://learnyouahaskell.com/syntax-in-functions
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.