3

i have this code:

  qs [] = []
  qs [x] = [x]
  qs (x:xs) = qs [a | a <- xs, a < x] ++ [x] ++ qs [b | b <- xs, b >= x]

when i save it to some file .hs and load it to ghci i can execute qs with a list of numbers without a problem.

but in Org-mode it give me error: *** Exception: <interactive>:16:1-56: Non-exhaustive patterns in function qs

the code block looks like this:

#+BEGIN_SRC haskell
qs [] = []
qs [x] = [x]
qs (x:xs) = qs [a | a <- xs, a < x] ++ [x] ++ qs [b | b <- xs, b >= x]
qs [2,9,3,8,4,7,5,6]
#+END_SRC

it's the same code as in the .hs file except the last line that execute an actual qs function.
how can i successfully execute this block?

Azriel
  • 181
  • 4

1 Answers1

5

Well i got answer from #haskell in freenode (thanks dmwit!).
the problem is that the REPL run the code line by line (and not as a whole), thus every line create a new qs, shadowing the old one.
so what i need to do is add a parentheses before and after the code, like that:

#+BEGIN_SRC haskell
:{
qs [] = []
qs [x] = [x]
qs (x:xs) = qs [a | a <- xs, a < x] ++ [x] ++ qs [b | b <- xs, b >= x]
:}
qs [2,9,3,8,4,7,5,6]
#+END_SRC

after this change, the code is executing without a problem.

Azriel
  • 181
  • 4
  • 1
    Yes, but this isn't really a good solution. What's really bad is I can't write functions without the :{ ... :} trick -- which means this isn't really tangle-able code, i.e., if you separate out the code into a .hs file, you've got to then strip out the trick. This is an old issue. There used to be `let` in front of function declarations and `:set +m` to allow multi-line input. But this defeats the purpose of a babel code block allowing true functions in the language to be evaluated. ob-haskell needs to be improved. – 147pm May 29 '19 at 03:25