5 Oct 12:06 2015

Hi.

Consider this structure:

vs :: Rational -> [Input] -> [[Output]]
vs _ []     = return []
vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - dur pm) xs pms :: Rational -> Input -> [Output] pms l x = [x, x+1, x+2, ...] -- Just an example, not real code. -- in reality, l is used to determine -- the result of pms. This is basically traverse, but with a state (l) added to it. So without the state, vs could be written as vs = traverse pms Now, I want to add Either e to this, like: vs :: Rational -> [Input] -> Either e [[Output]] pms :: Rational -> Input -> Either e [Output] However, I have no idea how to implement vs. Interestingly, adding Either e to vs without changing the code lets it compile, but it gives me the wrong result: vs :: Rational -> [Input] -> Either e [[Output]] vs _ [] = return []  (Continue reading) 4 Oct 17:18 2015 File download using wreq. Hi, Please can someone explain how,using the wreq package, I can download and save a binary file? Say the file is at the end of http://x.y.z/files/myfile.jpg and it is a jpeg and no authentication is needed. I just want to 1. Check that the URL is syntactically correct - flag and error if not 2. If the URL is syntactically ok then download the file using GET. 3. Check that the response code is 200 and if so save the file 3a. if the response code is not 200 then delegate to an error handling function or some simple idiomatic way of error handling. Thanks once again. Mike  3 Oct 11:12 2015 Parsing keywords Hi, What is the idiomatic way of parsing keywords, in a case agnostic fashion, for a ‘mini-language’? For example I’m creating some structured text… project{ env{ } setup{ } run{ } } that has ‘project’, ‘env’, ‘setup’ and ‘run’ as keywords and I want to parse in such a way that ‘PROJECT’, ‘ProJect’ etc are all recognised as the keyword ‘project’ similarly for ‘env’ etc. I’m using monadic rather than applicative parsing. Many Thanks Mike  (Continue reading) 28 Sep 17:15 2015 Haskell package maturity Since starting to look on Hackage for packages for such vital things as queues and algorithms, I am surprised to see very low numbers of downloads for packages that seem to me to be vitally important. For example, queuelike has only been downloaded 1617 times since being uploaded in 2009. Similar very low numbers seem to apply for many packages. Another example is cubicspline with only 485 downloads. My question is, are the numbers on Hackage correct, and if so, do they indicate hardly anybody uses them, or indeed Haskell? I am starting to wonder. I also notice version numbers are very low, often less than one and most often around 0.1 or so. This is either a display of extreme modesty on the part of Haskell library code developers (in fact, often found in open source communities), or an indication of lack of maturity of the code. Overall I am puzzled about this. I am trying to establish what packages to use in my coding and there seems to be little indication of what to choose, and how to assess code maturity. What am I missing? Andrew <div><div><div> <div>Since starting to look on Hackage for packages for such vital things as queues and algorithms, I am surprised to see very low numbers of downloads for packages that seem to me to be vitally important. For example, queuelike has only been downloaded 1617 times since being uploaded in 2009. Similar very low numbers seem to apply for many packages. Another example is cubicspline with only 485 downloads.</div> <div><br></div> <div>My question is, are the numbers on Hackage correct, and if so, do they indicate hardly anybody uses them, or indeed Haskell? I am starting to wonder.</div> <div><br></div> <div>I also notice version numbers are very low, often less than one and most often around 0.1 or so. This is either a display of extreme modesty on the part of Haskell library code developers (in fact, often found in open source communities), or an indication of lack of maturity of the code. Overall I am puzzled about this. I am trying to establish what packages to use in my coding and there seems to be little indication of what to choose, and how to assess code maturity. What am I missing?</div> <div><br></div> <div>Andrew</div> <div><br></div> <div><div></div></div> </div></div></div>  30 Sep 06:22 2015 State and Maybe and monad transformers Greetings All, I am writing code using a BankersDequeue from Data.Dequeue. I’d like to wrap the push and pop operations in a state monad for the standard reason, to avoid passing the dequeue around between function calls, and regard it as state instead, which it is. Wishing to avoid throwing a runtime error if the queue is empty (for the general case of queues) I have written a reduction of the concept using a crude stack to experiment, which uses State and Maybe. I notice that there are very few examples of using State and Maybe together. My questions are: Is this a faulty design pattern? Should this be done with monad transformers? Are there examples to be found of using State with Maybe as a monad transformer combination? Why is this pattern relatively rare, it seems? Is this program on the right track? Andrew — snip module Main where import Control.Monad.State type Stack = [Int] popIt :: Stack -> (Maybe Int, Stack) popIt [] = (Nothing, []) popIt (x:xs) = (Just x, xs) pop :: State Stack (Maybe Int) pop = state popIt push :: Int -> State Stack (Maybe ()) push a = state$ \xs -> (Just (), a:xs)

main = do
let a = evalState (do
push 3
push 2
push 1
pop
pop
a <- pop
return a
) []
print a

— snip

_______________________________________________
Beginners mailing list

29 Sep 16:10 2015

Help with a data type declaration

How can I interpret the following data type declaration? The book where
simple examples. This is what I can say about it:

* "Tree" is the name of the new type.
* "Branch" and "Leaf" are the type constructors.
* What is "a" and "b"?
* It seems to me that this type is kind of "recursively" defined but I
do not know exactly.

data Tree a b = Branch b (Tree a b) (Tree a b)
| Leaf a

I will very much appreciate your feedback.

Regards,
JAMB

26 Sep 01:18 2015

Get max element of a list using foldl or foldr

Hello,

I am a complete Haskell beginner and I am doing some exercises of my
book but I am stuck with the following:

Define
myMax :: Ord a => [a] -> a
which returns the maximum element of a list.

I must use foldl1 or foldr1 and I am given the hint to use max which
gets the maximum of 2 elements.

I will very much appreciate if you help me solve it.

JAMB


24 Sep 18:04 2015

foldl on Bool:s

I understand that with the function

sum'
:: (Num a) => [a] -> a

sum' = foldl (+) 0

the call

ghci>>> sum' [1,2,3]

will be evaluated as

0 + 1 + 2 + 3 = 6

elem'
:: (Eq a) => a -> [a] -> Bool
elem' y ys = foldl (\acc x -> if x == then True else acc) False ys

and calling it with

ghci>>> elem' 3 [1,2,3]

How is that evaluated to True by foldl in elem'?

Thanks in advance for any explanation to this!
<div><div dir="ltr">
<div>
<div>
<div>
<div>
<div>
<div>
</div>I understand that with the function<br><span><span><br><span>sum'</span></span><span><span class="">&nbsp;::&nbsp;</span><span>(</span><span class="">Num</span><span>&nbsp;a)</span><span class="">&nbsp;=&gt;&nbsp;</span><span>[a]</span><span class="">&nbsp;-&gt;&nbsp;</span><span>a </span></span></span><span><br>sum'<span class="">&nbsp;=&nbsp;</span><span>foldl&nbsp;(+)&nbsp;</span><span class="">0</span><span><br></span><span><span></span></span></span><br>
</div>
<div>the call<br>
</div>
<div><br></div>ghci&gt;&gt;&gt; sum' [1,2,3]<br><br>
</div>will be evaluated as<br><br>0 + 1 + 2 + 3 = 6<br><br>
</div>But what about the function <span><span><br><br><span>elem'</span></span><span><span class="">&nbsp;::&nbsp;</span><span>(</span><span class="">Eq</span><span>&nbsp;a)</span><span class="">&nbsp;=&gt;&nbsp;</span><span>a</span><span class="">&nbsp;-&gt;&nbsp;</span><span>[a]</span><span class="">&nbsp;-&gt;&nbsp;</span><span class="">Bool</span><span>&nbsp; <br></span></span></span><span>elem'&nbsp;y&nbsp;ys<span class="">&nbsp;=&nbsp;</span><span>foldl&nbsp;(\acc&nbsp;x</span><span class="">&nbsp;-&gt;&nbsp;</span><span class="">if</span><span>&nbsp;x</span><span class="">&nbsp;==&nbsp;</span><span>y&nbsp;</span><span class="">then</span><span>&nbsp;</span><span class="">True</span><span>&nbsp;</span><span class="">else</span><span>&nbsp;acc)&nbsp;</span><span class="">False</span><span>&nbsp;ys</span></span><br><br>
</div>and calling it with<br><br>
</div>ghci&gt;&gt;&gt; elem' 3 [1,2,3]<br><br>
</div>How is that evaluated to True by foldl in elem'?<br><div><div><div>
<div><br></div>
<div>Thanks in advance for any explanation to this!<br>
</div>
</div></div></div>
</div></div>

24 Sep 16:11 2015

Optimising a combinatorial algorithm?

Hi!

TL;DR: My implementation looks elegant (to me), but is rather slow
compared to a well optimized C++ impl. of the same algorithm.  Runtime
differences are currently huge.  Looking for input on what I could
change to match C++ performance, without loosing too much expressiveness
of the code.

I am writing software to deal with Braille music code.
As an exercise for me, and as an attempt to boil the algorithm down
to a more readable form, I am in the process of translating my C++
program[1] to Haskell.  So far, so good.  Given that I only came back to
Haskell (after 10 years of a break) roughly 2 weeks ago, I think I
have made quite nice progress.  However, now that I reached a stage
where I can actually do a performance comparison, the dreaded thing has
happened: The performance of my Haskell implementation is rather
horrible to what my C++ implementation can deliver.  While the code size
has gone down roughly by a factor of 10, runtime has gone up roughly by
the same factor...

For the impatient, here is the code I am talking about:
https://github.com/mlang/hbmc/blob/master/Data/Braille/Music.hs

The first half of the code is concerend with parsing braille.
Performance is not critical here, so I opted to use Parsec, mostly
because I am interested in good quality error messages.

The fun starts with the definition of 'pvs'.

The problem:
Braille music values are inherently ambiguous.  The sign for a whole
note can also mean a 16th note, a sign for a half note can also
mean a 32th note, the sign for a quarter note can also mean a 64th, and
a 8th note could also mean a 128th note.  This is histoical, and there
is no way around this.  The time signature (meter) is used to actually
tell which note is which.  This is relatively easy to do for an
experienced human, but quite complicated for a computer.
A measure (bar) of braille music can contain parallel *and* sequential
parts.  The data structure is basically:

data Sign = ...
type PartialVoice = [Sign]
type PartialMeasure = [PartialVoice]
type Voice = [PartialMeasure]
type Measure = [Voice]

As a constraint, all PartialVoices in a PartialMeasure need to have the
exact same duration.  Similarily, all Voices inside a Measure also need to
have the exact same duration.

So 'pvs', 'pms', 'vs' and 'ms' in my Data.Braille.Music module are concerned
with calculating all possible interpretations of the input.

All in all, the current Haskell implementation is doing what it should.
However, profiling tells me I am allocating roughly 2GB of memory
to disambiguate a single measure of music in 3/2 time.
This takes roughly 1.2s on my rather fast CPU.

The amount of allocation and the runtime are unacceptable.
If I leave it at that, I might as well ditch the code and terminate the
experiment.
As a comparison, my C++ impl. takes roughly 1s to disambiguate 32 measures of
one of these measures.

Profiling tells me I am spending 90% of all the time in 'dur', which is my
small helper method to calculate the duration of a PartialVoice,
PartialMeasure or Voice.

The project is setup to support profiling:

git clone https://github.com/mlang/hbmc
cd hbmc
cabal run profiling

Do you see a way to significantly speed this algorithm up, without
loosing a lot of expressiveness/elegancy?

Since I am back to Haskell since only 2 weeks, I am basically happy abut
every sort of hint/tip you can give me.  Stylistic, as well as
performance-related.

For now, this is really just a toy project.  However, if I can manage to
improve the performance significantly without destroying the readability
of the code too much, I might be interested to finish translating my
current C++ work into a more-or-less complete Haskell library.
After all, BMC is more or less a compiler.
And Haskell is supposed to be very good at this stuff

Here is a link to the original C++ project:
[1] https://github.com/malng/bmc

--

--
CYa,
⡍⠁⠗⠊⠕
_______________________________________________
Beginners mailing list

21 Sep 20:23 2015

Numeric Integer vs Integer

Hello!

Could someone explain to me the difference between the following?

data Atype = Numeric Integer | A | B C

and

data Atype = Integer | A | B C

<div><div dir="ltr">
<div>
<div>
<div>
<div>Hello!<br><br>
</div>Could someone explain to me the difference between the following?<br><br>
</div>
<span>data Atype = Numeric Integer | A | B C<br></span><span><br></span>
</div>
<span>and</span><span><br><br>data Atype = Integer | A | B C</span><br><br>

19 Sep 03:00 2015

Greetings All,

While I admire Haskell enormously, as a an intermediate beginner I find it difficult to know what is normal Haskell style for real world programming. On the subject of monad transformers, the paper by Martin Grabümller titled 'Monad Transformers Step by Step' gives an example of an evaluator using monad transformers with the following type:

type Eval6 α = ReaderT Env (ErrorT String (WriterT [String] (StateT Integer IO))) α

Is this how normal Haskell is developed and written in practice? I find the type and the function impenetrably dense and difficult to understand. Should I be aspiring to have my functions look and work like this? Of course it depends on what you want to do, but the essence of the question is, does Haskell ultimately end up looking like this for any real programming, beyond textbook list manipulation functions?

Andrew

<div>
<div>
<div>Greetings All,</div>
<div><br></div>
<div>While I admire Haskell enormously, as a an intermediate beginner I find it difficult to know what is normal Haskell style for real world programming. On the subject of monad transformers, the paper by Martin Grab&uuml;mller titled 'Monad Transformers Step by Step' gives an example of an evaluator using monad transformers with the following type:</div>
<div><br></div>
<div>type Eval6 &alpha; = ReaderT Env (ErrorT String (WriterT [String] (StateT Integer IO))) &alpha;</div>
<div><br></div>
<div>Is this how normal Haskell is developed and written in practice? I find the type and the function impenetrably dense and difficult to understand. Should I be aspiring to have my functions look and work like this? Of course it depends on what you want to do, but the essence of the question is, does Haskell ultimately end up looking like this for any real programming, beyond textbook list manipulation functions?</div>
</div>
<div><br></div>
<div>Andrew</div>
<div><br></div>
</div>


Gmane