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:  help using Data.Map lookup - how to get values       after the
      "Just" constructor (Martin Tomko)
   2. Re:  help using Data.Map lookup - how to get      values after the
      "Just" constructor (Daniel Fischer)
   3. Re:  help using Data.Map lookup - how to get values       after the
      "Just" constructor (Martin Tomko)
   4. Re:  haskell scheduling (aditya siram)
   5. Re:  help using Data.Map lookup - how to get      values after the
      "Just" constructor (Daniel Fischer)


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

Message: 1
Date: Tue, 21 Sep 2010 15:50:46 +0200
From: Martin Tomko <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get values      after the "Just" constructor
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Thank you Edgar, Daniel,
these helped a lot!
one more question regarding the suggestion of daniel, using ! if I KNOW 
that the key will occur (and as I work in a closed world, it should be 
the case).

Out of curiosity - how does one use ! ??

let's say I have a function returning a map: myFunct a b, and I was using:

M.lookup c (myFunct a b)

On this, I could happily use fromJust (M.lookup c (myFunct a b))

Now, if I do

(myFunct a b) ! c i get ! not in scope.

! seems to come from Data.Map and I import it as
import qualified Data.Map as M

I don't think I can do M.!   :))). How to go about it?

On 9/21/2010 3:34 PM, Daniel Fischer wrote:
> On Tuesday 21 September 2010 15:05:19, Martin Tomko wrote:
>    
>> Dear All,
>> as a newbie, I am only just discovering some intricacies of haskell.
>> I have a Data.Map map, and am trying the lookup function to get the
>> value for a given key (it is a list in my case, btw). I am struggling to
>> get access to the value, as it is constructed using Just. I know that
>> the question is therefore more general then the application on Map, so I
>> would be glad to get a wider picture. I Checked in Real World Haskell,
>> btu did nto find and answer. In Haskell the craft of... I found the
>> following (p263):
>>
>> mapValue :: (a->b)->  Maybe a ->  Maybe b
>> mapValue g (Just a) = Just (g a)
>> mapValue g Nothing = Nothing
>>      
> Note: this is a special case of the more general `fmap'. If you use fmap
> instead of mapValue, a) you don't need to write the function yourself (it's
> available from the Prelude) and b) it works unchanged if you find that
> instead of Maybe, e.g. a list is more appropriate.
>
>    
>> Which is fine, but it makes the Just constructor travel through the
>> whole code, which is annoying. Is there a way out? Or would that be a
>> dirty hack?
>>      
> No, not a dirty hack. But you have to distinguish between the cases where
> your lookup was successful and those where it wasn't.
> Depending on the surrounding code, for example
>
> .... case Data.Map.lookup key map of
>          Nothing ->  stuff for absent key
>          Just val ->  stuff with val
>
> is a good way. If you *know* that the key you want to look up is in the
> map, you can also use (!) to get the value.
>
>    
>> I do not quite understand the following discussion of maybe (p263-4),
>> but it seems like the code suggested is able to return a value at the
>> end...
>>      
> maybe :: b ->  (a ->  b) ->  Maybe a ->  b
> maybe default transform valueOrNot
>
> if valueOrNot is a value (Just val), the result is (transform val), if
> valueOrNot is Nothing, the result is the default.
>
> There's a less general variant (to be imported from Data.Maybe)
>
> fromMaybe :: a ->  Maybe a ->  a
> fromMaybe default valueOrNot
>
> returns val in case valueOrNot is (Just val) and the default in case of
> Nothing.
>
> If you use that, you might as well directly use Data.Map.findWithDefault.
>
> These are good if there's a sensible default for absent keys, otherwise the
> won't help you either.
>
>    
>> Thanks
>> Martin
>>      
>
>    




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

Message: 2
Date: Tue, 21 Sep 2010 16:18:12 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get     values after the "Just" constructor
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Tuesday 21 September 2010 15:50:46, Martin Tomko wrote:
> Thank you Edgar, Daniel,
> these helped a lot!
> one more question regarding the suggestion of daniel, using ! if I KNOW
> that the key will occur (and as I work in a closed world, it should be
> the case).
>
> Out of curiosity - how does one use ! ??
>
> let's say I have a function returning a map: myFunct a b, and I was
> using:
>
> M.lookup c (myFunct a b)
>
> On this, I could happily use fromJust (M.lookup c (myFunct a b))
>
> Now, if I do
>
> (myFunct a b) ! c i get ! not in scope.
>
> ! seems to come from Data.Map and I import it as
> import qualified Data.Map as M
>
> I don't think I can do M.!   :))).

Too ugly?

> How to go about it?

You'd use

myFunct a b M.! c

if you have

import qualified Data.Map as M

at the top of your module.
However, qualified operators are often ugly, so I suggest

import qualified Data.Map as M
import Data.Map (Map, (!))
-- to get the type name and the (!) operator for unqualified use

myFunct a b ! c

or, with parentheses,

(myFunct a b) ! c



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

Message: 3
Date: Tue, 21 Sep 2010 16:26:16 +0200
From: Martin Tomko <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get values      after the "Just" constructor
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

whoa, that's a cool suggestion, thanks!
I finally understand the meaning of importing only some functions 
explicitly.
Very cool.

thanks
Martin

On 9/21/2010 4:18 PM, Daniel Fischer wrote:
> On Tuesday 21 September 2010 15:50:46, Martin Tomko wrote:
>    
>> Thank you Edgar, Daniel,
>> these helped a lot!
>> one more question regarding the suggestion of daniel, using ! if I KNOW
>> that the key will occur (and as I work in a closed world, it should be
>> the case).
>>
>> Out of curiosity - how does one use ! ??
>>
>> let's say I have a function returning a map: myFunct a b, and I was
>> using:
>>
>> M.lookup c (myFunct a b)
>>
>> On this, I could happily use fromJust (M.lookup c (myFunct a b))
>>
>> Now, if I do
>>
>> (myFunct a b) ! c i get ! not in scope.
>>
>> ! seems to come from Data.Map and I import it as
>> import qualified Data.Map as M
>>
>> I don't think I can do M.!   :))).
>>      
> Too ugly?
>
>    
>> How to go about it?
>>      
> You'd use
>
> myFunct a b M.! c
>
> if you have
>
> import qualified Data.Map as M
>
> at the top of your module.
> However, qualified operators are often ugly, so I suggest
>
> import qualified Data.Map as M
> import Data.Map (Map, (!))
> -- to get the type name and the (!) operator for unqualified use
>
> myFunct a b ! c
>
> or, with parentheses,
>
> (myFunct a b) ! c
>
>
>    




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

Message: 4
Date: Tue, 21 Sep 2010 09:52:59 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] haskell scheduling
To: Nowshad <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Not sure if this is what you're looking for but it sounds like you
need some kind of zip :
zip [0..] [Just (T4 3 1 3 1), Just (T2 5 1), Nothing]
=> [(0,Just (T4 3 1 3 1)),(1,Just (T2 5 1)),(2,Nothing)]

-deech


On Thu, Sep 16, 2010 at 7:08 PM, Nowshad <[email protected]> wrote:
> Hi
> Hope that You are doing fine. I am stuck at this problem if you have any
> idea kindly help me out.
> I want to output the schedule for a given task and method for one hyper
> period....like (o, just (T4 3 1 3 1), (1, Just (T2 5 1 51) etc
>
> Regards
> Nowshad
> data Task = T4 Phase Period Execution Deadline
>   | T2 Period Execution
>     deriving (Show ,Eq)
> type Phase     = TimeUnit
> type Period    = TimeUnit
> type Execution = TimeUnit
> type Deadline  = TimeUnit
> type TimeUnit  = Int
> data Method = RM
>     | DM
>     | EDF
>       deriving (Show , Eq)
>  periodicSchedule :: Method -> [Task] -> [(TimeUnit, Maybe Task)]
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


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

Message: 5
Date: Tue, 21 Sep 2010 16:58:12 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] help using Data.Map lookup - how to
        get     values after the "Just" constructor
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Tuesday 21 September 2010 15:37:35, Martin Tomko wrote:
> Hi edgar,
> the whole of my code and the input data are one contained world, I am
> not using mondas - as I don't understand them properly,

Understanding will improve if you start to use them (don't go in too deep 
too fast, just start tinkering a little with Monads in Maybe and [], take a 
look at Control.Monad.State, build up experience and understanding).

> and they seem
> not to be necessary for the simple algebra I am trying to develop. Do I
> really need to use them here?

Not really. Monads make many things more convenient, but you can do almost 
all without them.
In the case at hand, using Maybe's Monad instance could add some 
convenience and elegance, but it'd not make a huge difference.

>
> Now, to your example:
>  >>>Try for example in ghci: fromJust $ Just 1 >>= (\x -> return $ x +
> 1) (I also had to search for definition of the $ operator, totally
> avoided in the two books I have, and seems to be just syntactic sugar
> instead of parentheses. Argh.)

Apart from avoiding parentheses, ($) has a few other uses, it can be passed 
to some higher order functions and it's nice to use for sections,

map ($ 4) [sin, cos, (^2)]

zipWith ($) [sin, cos, (^2)] [1,2,3]

In both cases, ($) could be replaced with id, since

($) :: (a -> b) -> a -> b
f $ x = f x

, ($) *is* the identity function, restricted to function types and with a 
different fixity, but somehow map ($ 4) looks less intriguing than
map (`id` 4), doesn't it?

>
> this seems to be equivalent to fromJust (Just 1),

It's fromJust (Just 2).

foo :: Int -> Maybe Int
foo x = Just (x+1)

The function (\x -> return $ x+1) is just foo, only more general (works for 
any Num type and any Monad).

Just val >>= func = func val, so
Just 1 >>= foo = foo 1 = Just (1+1) = Just 2

Nothing >>= _ = Nothing

> where I would assuem a
> result of 1. But the example seems to be dependent on whatever x is
> entered by keyboard. Am I right?

No, the x is (bound to) the argument of Just on the left of the (>>=), in 
this case 1.

Keyboard input lives in the IO Monad, there's no such stuff available in 
the Maybe Monad.

In

getLine >>= \line -> putStrLn (reverse line)

the name line is bound to the keyboard input.

Just "A line" >>= \line -> putStrLn (reverse line)

doesn't type-check, because the Monad to the left of (>>=) is different 
from the Monad on the right (left: Maybe, right: IO).

>
> Cheers
> Martin



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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 49
*****************************************

Reply via email to