Haskell greedy elem? - haskell

I love to use elem in haskell for catching IP:Port. However elem is "exact match", and I cannot find a way to split with : as delimiter? Any idea to work around this?
Kind Regards

Use break:
Prelude> break (==':') "192.168.0.1:5000"
("192.168.0.1",":5000")
Prelude> break (==':') "192.168.0.15"
("192.168.0.15","")

You might like the split package
bash> cabal update && cabal install split
bash> ghci
ghci> import Data.List.Split
ghci> splitOn ":" "192.168.0.1:9000"
["192.168.0.1","9000"]

Related

How can I run a GHCi statement in cabal v2-repl directly from command line?

How can I replicate ghci -e "print 123" in cabal v2-repl?
I've searched for "expression" or "statement" in cabal v2-repl --help with no luck.
The simplest way is to use shell piping capabilities. See:
% cabal v2-repl <<< ':type zip'
...
λ zip :: [a] -> [b] -> [(a, b)]
λ Leaving GHCi.
This <<< notation, in sh script interpreter, means that the quoted string is sent to the standard input of the command, followed by newline, then end of file.
There are other ways. For example, if you wish to supply more lines, you can use the so-called "here-doc":
% cabal repl <<EOF
:type zip
:type fst
EOF
The <<< notation is a shorthand for a one-line "here-doc".
In general, a program may know whether its standard input is a terminal (assuming, live user) or a file (which heredoc pretends to be), and behave differently. But ordinarily it would work either way, and, if you can send things to its standard input, you can automate it.

How to run GHCi in command line like a regular shell command

Is there any way I can run GHCi on command line like a regular command in shell?
For example: :browse in GHCi - list all the function for specific module.
but I want to run it on shell, e.g.: ghci --browse "MyModule"
which lists all the functions for the module
I know hoogle can run it on shell, e.g: hoogle Monad
The easiest way is to pipe in the commands via standard input. In Bash this can be done nicely with a “here-string”:
$ ghci <<< ':t reverse'
GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/sagemuej/.ghci
Loaded GHCi configuration from /home/sagemuej/.ghc/ghci.conf
Prelude> reverse :: [a] -> [a]
Prelude> Leaving GHCi.
Use verbosity 0 to avoid all the greeting stuff:
$ ghci -v0 <<< ':t reverse'
reverse :: [a] -> [a]

latexpdf-esque functionality in haskell?

My haskell program produces .tex output using my own module.
I can't see my way around having to run 'pdflatex' everytime I want to see a result. I can hardly believe this cannot be done in a more direct manner;
Is there a way to have my code compile the .tex-file?
Is the System.Process package what you are looking for? You can use the function
system :: String -> IO ExitCode
to perform system calls.
$ touch tempfile.txt
$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
>> import System.Process
>> system "ls"
tempfile.txt
ExitSuccess
Not particularly idiomatic, but it gets the job done.

Restricting output of hoogle on ghci

I have hoogle installed on my box and I can use hoogle from within ghci. Is there a way to ask hoogle to search only within certain modules and packages?
I tried
:hoogle +System.Directory directory
but I get a lot of results that are not found in the System.Directory.
How is your :hoogle command is defined in your .ghci file?
I have:
:def hoogle \x -> return $ ":!hoogle \"" ++ x ++ "\""
and your example works for me.
To debug the problem further, try (note the !):
:!hoogle +System.Directory directory
This directly executes the hoogle function on your system - so if that doesn't work it's a problem with your hoogle executable.

Simple library to do UTF-8 in Haskell (since Streams no longer compile)

I just want to read (and maybe write) UTF-8 data. haskell.org still advertises System.Streams which does not compile with recent ghc:
% runhaskell Setup.lhs configure
Configuring Streams-0.2.1...
runhaskell Setup.lhs build
Preprocessing library Streams-0.2.1...
Building Streams-0.2.1...
[10 of 45] Compiling System.FD ( System/FD.hs, dist/build/System/FD.o )
System/FD.hs:138:22:
Couldn't match expected type `GHC.IOBase.FD'
against inferred type `FD'
In the first argument of `fdType', namely `fd'
In a 'do' expression: fd_type <- fdType fd
In the expression:
let
oflags1 = case mode of
ReadMode -> ...
WriteMode -> ...
ReadWriteMode -> ...
AppendMode -> ...
binary_flags | binary = o_BINARY
| otherwise = 0
oflags = oflags1 .|. binary_flags
in
do fd <- fdOpen filepath oflags 438
fd_type <- fdType fd
when (mode == WriteMode && fd_type == RegularFile)
$ do fdSetFileSize fd 0
....
Similar problem with Streams 0.1. I cannot get more recent versions since the official site is down:
% wget http://files.pupeno.com/software/streams/Streams-0.1.7.tar.bz2
--2009-07-30 15:36:14-- http://files.pupeno.com/software/streams/Streams-0.1.7.tar.bz2
Resolving files.pupeno.com... failed: Name or service not known.
wget: unable to resolve host address `files.pupeno.com'
A better solution? darcs source code?
Use the utf8-string or the more recent text package.
View the list of packages on hackage.
Edit:
L. Kolmodin is right: utf8-string or text is the right answer. I'll leave my original answer below for reference. Google seems to have steered me wrong in choosing IConv. (The equivalent of my IConv wrapper function is already in utf8-string as Codec.Binary.UTF8.String.encodeString.)
Here is what I've been using--I may not remember the complete solution, so let me know if you still run into problems:
From Hackage, install IConv. Unfortunately, Codec.Text.IConv.convert operates on bytestrings, not strings. I guess you could read files directly as bytestrings, but I wrote a converter since HaXml uses normal strings:
import qualified Data.ByteString.Lazy.Char8 as B
utf8FromLatin1 = B.unpack . convert "LATIN1" "UTF-8" . B.pack
Now, on Mac OS, you have to compile with
$ ghc -O2 --make -L/usr/lib -L/opt/local/lib Whatever.hs
Because there was some library conflict, I think with MacPorts, I have to point explicitly to the built-in iconv libraries. There is probably a way to always pass those -L flags to ghc, but I haven't looked it up yet.
utf-8 strings are just byte character sequences, so it should be possible to just read and write the strings as is. All of the first 127 characters, including whitespace, should be ascii. Of course, you would need your own functions for manipulating strings since they are now multi byte sequences.

Resources