Question

I am able to run the code below in terminal which is in respondPalindromes.hs file but failing to test the same in emacs.

respondPalindromes = unlines . map (\xs -> if isPalindrome xs then "palindrome" else "not a palindrome") . lines
where isPalindrome xs = xs == reverse xs
main = interact respondPalindromes

Below is the terminal details:

optimight@optimight:~$ cat words.txt | runhaskell respondPalindromes.hs
not a palindrome
palindrome
palindrome
palindrome

Below is the Emacs haskell-mode details:

*Main> :load "/home/optimight/respondPalindromes.hs"
[1 of 1] Compiling Main ( /home/optimight/respondPalindromes.hs, interpreted )
Ok, modules loaded: Main.
*Main> :! cat words.txt | ./respondPalindromes.hs
/bin/sh: 1: ./respondPalindromes.hs: Permission denied
*Main> :! cat words.txt | ./respondPalindromes
/bin/sh: 1: ./respondPalindromes: not found
*Main>

Edit after Dietrich Epp's answer:

*Main> !: cat words.txt | runhaskell respondPalindromes.hs
:12:1: parse error on input `!:'

*Main> cat words.txt | runhaskell respondPalindromes.hs
:13:15: parse error on input `|'
*Main>

Was it helpful?

Solution

In the regular terminal window, you run this command:

cat words.txt | runhaskell respondPalindromes.hs

But in the Haskell window, you run the following shell command:

cat words.txt | ./respondPalindromes.hs

Why run a different command, if the first one works fine?

These are just ordinary shell commands. If you haven't set up respondPalindromes.hs as an executable, then it won't run as an executable, you have to use runhaskell in order to execute it.

Furthermore, if you run the program this way (by executing a shell command from inside the Haskell interactive shell), there's no point to using a fancy Haskell shell. You might as well be using an ordinary shell.

Instead, you may want to avoid the shell altogether, such as:

readFile "words.txt" >>= respondPalindromes >>= putStr

The >>= operator in Haskell is kind of like the | pipe in the shell.

Summary: The :! command executes an ordinary shell command, it doesn't have anything to do with Haskell.

OTHER TIPS

I don't know haskell but on command line you are executing runhaskell respondPalindromes.hs but inside emacs you are running ./respondPalindromes.hs.

Pretty sure that is the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top