Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: a problem with maps (David Place)
2. Re: a problem with maps (Dennis Raddle)
3. Re: a problem with maps (David Place)
4. Data.Typeable (Benjamin Edwards)
5. Re: a problem with maps (Christian Maeder)
6. The Fun of Programming, Chapter 6, How to Write a Financial
Contract (Costello, Roger L.)
7. Re: a problem with maps (Julian Porter)
8. I wrote a state monad example - help me make it more
Haskellic (Rohit Garg)
----------------------------------------------------------------------
Message: 1
Date: Sat, 23 Jul 2011 08:44:56 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] a problem with maps
To: Ertugrul Soeylemez <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jul 22, 2011, at 10:58 PM, Ertugrul Soeylemez wrote:
> fromAmbList :: Ord k => [(k, a)] -> Map k [a]
> fromAmbList = M.fromListWith (++) . map (second pure)
If I am reading your code, I may look up second and find it in Control.Arrow.
I have to learn about Arrows to understand your code? And pure makes me need
to study the Applicative class. I imagine that it is likely that second is the
only thing from Control.Arrow that you are using and pure is the only thing
from Control.Applicative. So, you need two lines of extra code to express what
could be expressed much more perspicuously as:
>
> fromAmbList :: Ord k => [(k, a)] -> Map k [a]
> fromAmbList = M.fromListWith (++) . map (\(k,v) -> (k,[v]))
>
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
------------------------------
Message: 2
Date: Sat, 23 Jul 2011 06:17:33 -0700
From: Dennis Raddle <[email protected]>
Subject: Re: [Haskell-beginners] a problem with maps
To: David Place <[email protected]>
Cc: [email protected], Ertugrul Soeylemez <[email protected]>
Message-ID:
<CAKxLvoo=r4ckpx66+yow7jxqb6dstq9bqzbwmgy99oq2uto...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
I understand your point, but the spirit of Haskell as I have encountered it
(as a beginner, reading Typeclasspedia and other sources) would be
- Of course you need to learn about arrows and applicative functors.You need
to do that anyway.
- The expression "map (second pure)" is simpler in the sense it has fewer
moving parts, and it's worth understanding how to read it and how to write
things like it
That's how I understand the spirit of Haskell, although I could have a
limited perspective.
Dennis
On Sat, Jul 23, 2011 at 5:44 AM, David Place <[email protected]> wrote:
>
> On Jul 22, 2011, at 10:58 PM, Ertugrul Soeylemez wrote:
>
> > fromAmbList :: Ord k => [(k, a)] -> Map k [a]
> > fromAmbList = M.fromListWith (++) . map (second pure)
>
> If I am reading your code, I may look up second and find it in
> Control.Arrow. I have to learn about Arrows to understand your code? And
> pure makes me need to study the Applicative class. I imagine that it is
> likely that second is the only thing from Control.Arrow that you are using
> and pure is the only thing from Control.Applicative. So, you need two lines
> of extra code to express what could be expressed much more perspicuously
> as:
>
> >
> > fromAmbList :: Ord k => [(k, a)] -> Map k [a]
> > fromAmbList = M.fromListWith (++) . map (\(k,v) -> (k,[v]))
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110723/d6eabef5/attachment-0001.htm>
------------------------------
Message: 3
Date: Sat, 23 Jul 2011 09:25:45 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] a problem with maps
To: Dennis Raddle <[email protected]>
Cc: [email protected], Ertugrul Soeylemez <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On Jul 23, 2011, at 9:17 AM, Dennis Raddle wrote:
> That's how I understand the spirit of Haskell, although I could have a
> limited perspective.
I think this is a valid point of view, but needs to be balanced by a need for
perspicuity. After all, you aren't really using Arrows. It's just a
coincidence that this function does what you want on a concrete implementation
level. When I was learning to use the HXT package for XML manipulation, I was
happy to learn about its use of Arrows. It really uses them. I need to
understand Arrows to use it.
To me, this kind of use reads like a bad pun. :-)
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110723/7a719035/attachment-0001.htm>
------------------------------
Message: 4
Date: Sat, 23 Jul 2011 15:01:16 +0100
From: Benjamin Edwards <[email protected]>
Subject: [Haskell-beginners] Data.Typeable
To: haskellbeginners <[email protected]>
Message-ID:
<CAN6k4nh=fmjr4drgpm+e2pnnnyrhs8yv_kfjfvqrxmeasuu...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hi All,
Does anyone have a good resource as to where I can learn more about this
module and its uses?
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110723/b3448ccb/attachment-0001.htm>
------------------------------
Message: 5
Date: Sat, 23 Jul 2011 16:08:25 +0200
From: Christian Maeder <[email protected]>
Subject: Re: [Haskell-beginners] a problem with maps
To: Dennis Raddle <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Am 23.07.2011 03:40, schrieb Dennis Raddle:
...
> listOutMap :: Ord k => Map k [a] -> [(k,a)]
> listOutMap m = concatMap (\(k,a) -> zip (repeat k) a) (M.toList m)
...
instead of "zip" and "repeat" I prefer just "map":
"\ (k, as) -> map (\ a -> (k, a)) as"
(and don't bother to switch on TupleSections for "(k,)")
C.
------------------------------
Message: 6
Date: Sat, 23 Jul 2011 10:44:20 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] The Fun of Programming, Chapter 6, How to
Write a Financial Contract
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hi Folks,
Chapter 6 is exciting. I learned a lot about how to write programs in a
functional way.
The chapter is written by Simon Peyton Jones. He is an excellent author.
I created slides that summarizes the chapter and the lessons that I learned:
http://www.xfront.com/The-Fun-of-Programming/Chapter6/How-to-write-a-financial-contract.pptx
/Roger
------------------------------
Message: 7
Date: Sat, 23 Jul 2011 15:48:03 +0100
From: Julian Porter <[email protected]>
Subject: Re: [Haskell-beginners] a problem with maps
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Lifting to the List monad gives a very elegant solution:
listOutMap :: k -> [a] -> [(k,a)]
listOutMap x ys = liftM2 (,) (return x) ys
Julian Porter
[email protected]
http://www.porternet.org
On 23 Jul 2011, at 15:08, Christian Maeder wrote:
> Am 23.07.2011 03:40, schrieb Dennis Raddle:
> ...
>> listOutMap :: Ord k => Map k [a] -> [(k,a)]
>> listOutMap m = concatMap (\(k,a) -> zip (repeat k) a) (M.toList m)
> ...
>
> instead of "zip" and "repeat" I prefer just "map":
>
> "\ (k, as) -> map (\ a -> (k, a)) as"
>
> (and don't bother to switch on TupleSections for "(k,)")
>
> C.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110723/1524bc1e/attachment-0001.htm>
------------------------------
Message: 8
Date: Sat, 23 Jul 2011 21:26:20 +0530
From: Rohit Garg <[email protected]>
Subject: [Haskell-beginners] I wrote a state monad example - help me
make it more Haskellic
To: [email protected]
Message-ID:
<CAC1T7gjE5+CvRNCiaB1AN_hwdc7vWpADkuE43QiNSdZY=m4...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I have written my first program using state monads and I would like
some feedback from the community regarding how Haskellic it is. It's a
simple simulator for a toy microprocessor. The contents of registers
are the state that needs to be preserved across instruction
evaluation. I have tried to implement the code dealing with monads
using both >>=, >> and the do block.
Also, I'd like to make the RegisterVal type Word32, but when I do that, then
================
initial_rf = [0,0,0,0]
================
this line fails to typecheck.
If there are any bugs or non-idiomatic use of haskell, please let me know.
Thanks and regards,
Rohit
=================
import Data.Word
import Control.Monad.State
type RegisterVal = Int
type RegisterFile = [RegisterVal]
type RegisterID = Int
--destination comes first
data Instruction = Add RegisterID RegisterID RegisterID
| Sub RegisterID RegisterID RegisterID
| Mov RegisterID RegisterID
| Movc RegisterID RegisterVal
deriving (Show)
get_operand :: RegisterFile -> RegisterID -> RegisterVal
get_operand registers regid = registers !! regid
store_operand :: RegisterFile -> RegisterID -> RegisterVal -> RegisterFile
store_operand registers regid value = (take regid registers) ++
[value] ++ (drop (regid+1) registers)
eval_inst :: Instruction -> RegisterFile -> RegisterFile
eval_inst (Add dstid src1id src2id) rf = rf_final
where rf_final = store_operand rf dstid res --store
res = op1 + op2 --evaluate
(op1, op2) = (get_operand rf src1id , get_operand rf
src2id) --load
eval_inst (Sub dstid src1id src2id) rf = rf_final
where rf_final = store_operand rf dstid res --store
res = op1 - op2 --evaluate
(op1, op2) = (get_operand rf src1id , get_operand rf
src2id) --load
eval_inst (Mov dstid srcid) rf = rf_final
where rf_final = store_operand rf dstid res --store
res = op1 --evaluate
op1 = get_operand rf srcid --load
eval_inst (Movc dstid imm) rf = rf_final
where rf_final = store_operand rf dstid res --store
res = imm --evaluate
initial_rf :: RegisterFile
initial_rf = [0,0,0,0]
insts = [Movc 0 231, Movc 1 (-42), Add 2 1 0, Sub 3 1 0]
execute_inst2 :: Instruction -> State RegisterFile RegisterFile
{-
execute_inst2 inst = get >>= \rf1 -> let rf2 = eval_inst inst rf1
in put rf2 >> return rf2
-}
execute_inst2 inst = do
rf1 <- get
let rf2 = eval_inst inst rf1
put rf2
return rf2
execute_program2 :: [Instruction] -> State RegisterFile RegisterFile
execute_program2 [] = do
rf <- get
put rf
return rf
execute_program2 (x:xs) = do
rf1 <- execute_inst2 x
rf2 <- execute_program2 xs
put rf2
return rf2
{-
execute_program2 [] = get >>= \rf ->
put rf >> return rf
execute_program2 (x:xs) = (execute_inst2 x) >>= \a ->
(execute_program2 xs) >>= \b ->
return b
-}
main :: IO ()
main = putStrLn $ show $ execState (execute_program2 insts) initial_rf
--
Rohit Garg
http://rpg-314.blogspot.com/
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 51
*****************************************