derek riemer | 31 Jul 17:30 2015
Picon

An unsigned int class.

Is there anything in haskell that represents an unsigned Int class? 
namely, something bounded below at 0? If not, is it possible to define 
an Int in this way to add the bounded constraint (I can't immagine 
trying to define Int like this, there has got to be a better way that I 
haven't quite understood in my study of haskell).
data UnsignedInt = 0 | 1 | 2 | (...) for a long time doing that ... 
deriving (Show, Eq, Ord, Bounded, Read)
Thanks,
Derek
derek riemer | 30 Jul 15:16 2015
Picon

How to define ord manually.

Hi,
I was playing around with haskell, and decided to implement a 
RockPaperScissors type.

data RPS= Rock | Paper | Scissors
Then I manually derived show for fun.
instance Show RPS where
     show Rock = "rock"
     show Paper = "paper"
     show Scissors = "scissors"

But, I have a problem when defining ord.
How do I look in the sourcecode for ord?
In ghci I did
prelude> :i Ord
<snip>
          Ord (a, b, c, d, e, f, g, h, i, j)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g, Ord h,
           Ord i) =>
          Ord (a, b, c, d, e, f, g, h, i)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g,
           Ord h) =>
          Ord (a, b, c, d, e, f, g, h)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f, Ord g) =>
          Ord (a, b, c, d, e, f, g)
   -- Defined in `GHC.Classes'
instance (Ord a, Ord b, Ord c, Ord d, Ord e, Ord f) =>
(Continue reading)

Timothy Washington | 25 Jul 19:30 2015
Picon

Haskell equivalent to Clojure's partition fn

Is there a Haskell equivalent to Clojure's partition function? I'm particularly interested in the stepwise feature, where we can select a certain `n` size of the list, then step `m` and take again. 

What's returned is a list of lists. Each list being a sublist incremented by `m`. I didn't see anything in the below packages. And I wanted to check before I go about writing my own.

Thanks 
Tim

<div><div dir="ltr">
<div>Is there a Haskell equivalent to <a href="http://clojuredocs.org/clojure.core/partition">Clojure's partition</a> function? I'm particularly interested in the stepwise feature, where we can select a certain `n` size of the list, then step `m` and take again.&nbsp;</div>
<div><br></div>
<div>What's returned is a list of lists. Each list being a sublist incremented by `m`. I didn't see anything in the below packages. And I wanted to check before I go about writing my own.<br>
</div>
<div><ul>
<li>
<a href="https://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#g:13">Prelude</a><br>
</li>
<li>
<a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html">Data.List</a><br>
</li>
<li>
<a href="http://hackage.haskell.org/package/array-0.5.0.0/docs/Data-Array.html">Data.Array</a><br>
</li>
<li>
<a href="http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data-Vector.html">Data.Vector</a><br>
</li>
</ul></div>
<div><br></div>
<div>Thanks&nbsp;<br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr">
<div>Tim</div>
<div><br></div>
</div></div></div></div></div>
</div>
</div></div>
Martin Vlk | 23 Jul 10:07 2015
Picon

(->) reader/environment

Hi, this one is a bit of a mystery to me.

I am working on solutions to cis194/NICTA Haskell exercises and among
others I am asked to work with (->).

I understand it refers to the reader/environment design patterns, but I
am stumped as for what exactly it is. It doesn't seem to be an ordinary
type, I know it is used as type constructor in type declarations, so
that suggests it's a function, but somehow special. Looking in Hoogle it
seems to be Haskell keyword - e.g. not an ordinary function.

So what is it - type constructor, a keyword, how is it related to the
reader/environment pattern?

From GHCI's :info (->) I get:
data (->) a b 	-- Defined in ‘ghc-prim-0.4.0.0:GHC.Prim’
instance P.Monad ((->) r) -- Defined in ‘GHC.Base’
instance P.Functor ((->) r) -- Defined in ‘GHC.Base’
instance P.Applicative ((->) a) -- Defined in ‘GHC.Base’
instance P.Monoid b => P.Monoid (a -> b) -- Defined in ‘GHC.Base’

Cheers
Martin
_______________________________________________
Beginners mailing list
Beginners <at> haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Ali Baharev | 23 Jul 00:14 2015
Picon

Re: Traversing generic trees

Dear Elliot,

The question is whether there is a (not too obscure) way to eliminate
the code duplication, or how the code duplication can be avoided using
nested tuples instead.

Please try to answer the question.

Ali

On Wed, Jul 22, 2015 at 11:51 PM, elliot <elliot <at> citrusbytes.net> wrote:
> In general you don't produce large trees (or any data structure) by hand. It's slightly frustrating to
test of course (as you've discovered) but for the most part you'll only need to produce small structures.
>
> --
> e: elliot <at> citrusbytes.net
> p: +44 751 070 5158
> ---- Original Message ----
>  From: "Ali Baharev"
>  To: beginners <at> haskell.org
>  Sent: Wed, Jul 22, 2015, 10:24 PM
>  Subject: [Haskell-beginners] Traversing generic trees
> Dear All,
>
> I have started learning Haskell recently, and I try to reimplement
> interesting algorithms that are challenging to do in other languages.
> I would like to ask you for your kind help with a problem that I am
> stuck with.
>
> A generic tree is given in the form of nested sequences (nested lists
> or nested tuples) and I would like to traverse it in (1) depth-first
> order,  (2) lazily and (3) without copying the data into some other
> datastructure first. I test the algorithm by pretty printing the tree.
> My goal is learning, so Data.Tree is not an option (although I did
> look into its implementation).
>
> Here is my first attempt:
>
> data Tree a = Leaf a | Node [Tree a]
>
> tree = Node [Leaf 'a', Node [Leaf 'b', Leaf 'c', Node []], Leaf 'd']
>
> toString :: (Show a) => Tree a -> String
> toString (Leaf leaf)  = " " ++ show leaf ++ " "
> toString (Node nodes) = " [ " ++ concatMap toString nodes ++ " ] "
>
> main = print $ toString tree
>
> It (more or less) satisfies the above requirements. The pretty
> printing is not very pretty but the part that I am really not happy
> with is the lot of code duplication in giving the tree. I would like
> to enter it as:
>
> tree = ['a', ['b', 'c', []], 'd']
>
> which of course won't work.
>
> Is there a (not too obscure) way to help the compiler so that it
> understands that 'a' means Leaf 'a', etc?
>
> I have also tried
>
> tree = ('a', ('b', 'c', ()), 'd')
>
> but then I do not know how to destructure the nested tuples.
>
> Any help is greatly appreciated.
>
> Ali
> _______________________________________________
> Beginners mailing list
> Beginners <at> haskell.org (mailto:Beginners <at> haskell.org)
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners (http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners)
Ali Baharev | 22 Jul 23:24 2015
Picon

Traversing generic trees

Dear All,

I have started learning Haskell recently, and I try to reimplement
interesting algorithms that are challenging to do in other languages.
I would like to ask you for your kind help with a problem that I am
stuck with.

A generic tree is given in the form of nested sequences (nested lists
or nested tuples) and I would like to traverse it in (1) depth-first
order,  (2) lazily and (3) without copying the data into some other
datastructure first. I test the algorithm by pretty printing the tree.
My goal is learning, so Data.Tree is not an option (although I did
look into its implementation).

Here is my first attempt:

data Tree a = Leaf a | Node [Tree a]

tree = Node [Leaf 'a', Node [Leaf 'b', Leaf 'c', Node []], Leaf 'd']

toString :: (Show a) => Tree a -> String
toString (Leaf leaf)  = " " ++ show leaf ++ " "
toString (Node nodes) = " [ " ++ concatMap toString nodes ++ " ] "

main = print $ toString tree

It (more or less) satisfies the above requirements. The pretty
printing is not very pretty but the part that I am really not happy
with is the lot of code duplication in giving the tree. I would like
to enter it as:

tree = ['a', ['b', 'c', []], 'd']

which of course won't work.

Is there a (not too obscure) way to help the compiler so that it
understands that 'a' means Leaf 'a', etc?

I have also tried

tree = ('a', ('b', 'c', ()), 'd')

but then I do not know how to destructure the nested tuples.

Any help is greatly appreciated.

Ali
Ryan Warner | 22 Jul 04:29 2015
Picon

seq vs deepseq

I'm trying to grok Haskell's laziness. In this example, why does the seq not force youTyped to wait for s to be fully evaluated, before returning a result to putStrLn?

--------------------
import System.IO (hSetBuffering,stdin,BufferMode(..))

youTyped :: String -> String
youTyped s = s `seq` "you typed: " ++ s

main = do
hSetBuffering stdin NoBuffering
s <- getContents
let l = map youTyped $ lines $ takeWhile ( /= '\04' ) s
mapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)
--------------------

The output looks like:

--------------------

$ runhaskell seqLazy.hs 

Start Typing (Ctrl-D to exit):

Hyou typed: Heelllloo  wwoorrlldd


fyou typed: faaiill


^D

--------------------

Changing seq, to deepseq does the trick though.

--------------------
import System.IO (hSetBuffering,stdin,BufferMode(..))
import Control.DeepSeq

youTyped :: String -> String
youTyped s = s `deepseq` "you typed: " ++ s

main = do
hSetBuffering stdin NoBuffering
s <- getContents
let l = map youTyped $ lines $ takeWhile ( /= '\04' ) s
mapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)
--------------------

Output:


$ runhaskell deepSeqLazy.hs 

Start Typing (Ctrl-D to exit):

Hello world

you typed: Hello world

success

you typed: success

^D


When does it make sense to use seq then?

<div><div dir="ltr">
<div>I'm trying to grok Haskell's laziness. In this example, why does the seq not force youTyped to wait for s to be fully evaluated, before returning a result to putStrLn?</div>
<div><br></div>
<div>--------------------</div>
<div>import System.IO (hSetBuffering,stdin,BufferMode(..))</div>
<div><br></div>
<div>youTyped :: String -&gt; String</div>
<div>youTyped s = s `seq` "you typed: " ++ s</div>
<div><br></div>
<div>main = do</div>
<div>
<span>	</span>hSetBuffering stdin NoBuffering</div>
<div>
<span>	</span>s &lt;- getContents</div>
<div>
<span>	</span>let l = map youTyped $ lines $ takeWhile ( /= '\04' ) s</div>
<div>
<span>	</span>mapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)</div>
<div>--------------------</div>
<div><br></div>
<div>The output looks like:</div>
<div><br></div>
<div>
<div>--------------------</div>
<p>$ runhaskell seqLazy.hs&nbsp;</p>
<p>Start Typing (Ctrl-D to exit):</p>
<p>Hyou typed: Heelllloo&nbsp; wwoorrlldd</p>
<p><br></p>
<p>fyou typed: faaiill</p>
<p><br></p>
<p>^D</p>
</div>
<div>--------------------</div>
<div><br></div>
<div>Changing seq, to deepseq does the trick though.</div>
<div><br></div>
<div>
<div>--------------------</div>
<div>import System.IO (hSetBuffering,stdin,BufferMode(..))</div>
<div>import Control.DeepSeq</div>
<div><br></div>
<div>youTyped :: String -&gt; String</div>
<div>youTyped s = s `deepseq` "you typed: " ++ s</div>
<div><br></div>
<div>main = do</div>
<div>
<span>	</span>hSetBuffering stdin NoBuffering</div>
<div>
<span>	</span>s &lt;- getContents</div>
<div>
<span>	</span>let l = map youTyped $ lines $ takeWhile ( /= '\04' ) s</div>
<div>
<span>	</span>mapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)</div>
</div>
<div>--------------------</div>
<div><br></div>
<div>Output:</div>
<div>
<p><br></p>
<p>$ runhaskell deepSeqLazy.hs&nbsp;</p>
<p>Start Typing (Ctrl-D to exit):</p>
<p>Hello world</p>
<p>you typed: Hello world</p>
<p>success</p>
<p>you typed: success</p>
<p>^D</p>
<p><br></p>
<p>When does it make sense to use seq then?</p>
</div>
</div></div>
Dimitri DeFigueiredo | 18 Jul 01:08 2015
Picon

Re: Cabal dependencies are per target or global?


I guess my example was incomplete. I can make GHC run. Here's a modified 
version where I do have the ghc-options line. I can build an executable 
for the second target (myPersonalMain) by commenting
the "build-depends: section" for the first target (agent).
My .cabal file with (commented lines) is:
------------------------------------------------
name:                agent
version:             0.1.0.0
build-type:          Simple
cabal-version:       >=1.20
------------------------------------------------
executable agent
   main-is:             Main.hs
   hs-source-dirs: ./src

   -- build-depends:      base >=4.6 && <4.7
   --                   , unordered-containers >= 0.2.3.0
   --                   , unix >= 2.6.0.1
   --                   , process >= 1.1.0.2
   --                   , stm >= 2.4.2

   -- Base language which the package is written in.
   default-language:    Haskell2010

----------------------------------------------
executable myPersonalMain
     main-is:            Mpm.hs
     hs-source-dirs:     ./src
     ghc-options:       -main-is Mpm
     build-depends:
                 base              >=4.4

     default-language:   Haskell2010
----------------------------------------------

The contents of Mpm.hs are:

module Mpm where
main = putStrLn "Hi!"

But if I remove the comments in the .cabal file above, I get this:

Dis-machine:cabal-tests dimitri$ cabal build myPersonalMain
./agent.cabal has been changed. Re-configuring with most recently used
options. If this fails, please run configure manually.
Resolving dependencies...
Configuring agent-0.1.0.0...
cabal: At least the following dependencies are missing:
process >=1.1.0.2,
stm >=2.4.2,
unix >=2.6.0.1,
unordered-containers >=0.2.3.0

Is this a bug? Or am I missing something?

Cheers,

Dimitri

On 17/07/15 15:10, Imants Cekusins wrote:
> this could shed some light:
>
> http://stackoverflow.com/questions/14238729/producing-multiple-executables-from-single-project
>
> answer #2 was marked as answered
>
> ghc-options:      -O2 -threaded -with-rtsopts=-N -main-is FirstExecutable

amindfv | 17 Jul 20:06 2015
Picon

Re: basic Functor, Applicative and Monad instances

Right, you'll want to say eg

fmap :: (a -> b) -> Val a -> Val b

Tom

El Jul 17, 2015, a las 12:23, Imants Cekusins <imantc <at> gmail.com> escribió:

>> InstanceSigs
> 
> Thank you Tom.
> 
> this sig does not work though:
> fmap ::(a -> b) -> f a -> f b
_______________________________________________
Beginners mailing list
Beginners <at> haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Imants Cekusins | 16 Jul 21:52 2015
Picon

basic Functor, Applicative and Monad instances

Here are complete working snippets with Functor, Applicative and Monad
instances. This is my first attempt to write own instances rather than
use ready ones.

Here we go:

module Part.Monad where

import Control.Applicative
{-# ANN module ("HLint: ignore Use let"::String) #-}

 {-
   Val' { val = b1 } can also be written as
   Val' b1
 -}

data Val a = Val' {
    val::a
}  deriving Show

data Weather = Cold | Warm deriving Show

instance Functor Val where
-- (a -> b) -> f a -> f b
   fmap ab fa = let  a1 = val fa
                     b1 = ab a1
                in Val' { val = b1 }

instance Applicative Val where
-- a -> f a
   pure a =  Val' { val = a }

-- f (a -> b) -> f a -> f b
   (<*>) fab fa =  let  ab1 = val fab
                        a1 = val fa
                        b1 = ab1 a1
                   in Val' { val = b1 }

instance Monad Val where
-- a -> m a
   return a = Val' { val = a }

--  m a -> (a -> m b) -> m b
   (>>=) ma amb = let a1 = val ma
                 in amb a1

-- pure and return in this example are interchangeable
main::Int -> IO()
main i = do                         -- do:   Val as monad
   v1 <- pure Val' { val = i }      -- pure: applicative

   v2 <- return $ over20 <$> v1     -- <$> : functor
   print v2

   v3 <- return $ Val' weather <*> v2  -- <*> : applicative
   print v3

over20::Int-> Bool
over20 i
   | i > 20 = True
   | otherwise = False

weather::Bool-> Weather
weather False = Cold
weather True = Warm
Sevcsik András | 12 Jul 20:31 2015
Picon

http-client: Access response body of error responses

Hi list,

Is there any way to read the response body of an >=400 HTTP response with http-client?

If the response is an error response, it throws a StatusCodeException [1], which only exposes the headers, not the whole Response value.

However, CouchDB gives a response body for the error responses, describing the error, which I want to access.


Cheers
--
Minden jót,
Sevcsik András
<div>
<div dir="ltr">Hi list,<div><br></div>
<div>Is there any way to read the response body of an &gt;=400 HTTP response with http-client?</div>
<div><br></div>
<div>If the response is an error response, it throws a StatusCodeException [1], which only exposes the headers, not the whole Response value.</div>
<div><br></div>
<div>However, CouchDB gives a response body for the error responses, describing the error, which I want to access.</div>
<div><br></div>
<div>[1]:&nbsp;<a href="https://hackage.haskell.org/package/http-client-0.2.1.1/docs/Network-HTTP-Client.html#v:StatusCodeException">https://hackage.haskell.org/package/http-client-0.2.1.1/docs/Network-HTTP-Client.html#v:StatusCodeException</a>
</div>
<div><br></div>
<div>Cheers</div>
</div>
<div dir="ltr">-- <br>
</div>
<div dir="ltr">Minden j&oacute;t,<div>Sevcsik Andr&aacute;s</div>
</div>
</div>

Gmane