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.  maybe in IO (vold)
   2. Re:  Shorten this code (Gesh hseG)
   3. Re:  maybe in IO (Krzysztof Skrz?tnicki)
   4. Re:  maybe in IO (Krzysztof Skrz?tnicki)
   5. Re:  maybe in IO (Gesh hseG)
   6. Re:  maybe in IO (Michael Orlitzky)
   7. Re:  Shorten this code (Henk-Jan van Tuyl)


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

Message: 1
Date: Thu, 1 Aug 2013 15:00:59 +0000 (UTC)
From: vold <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] maybe in IO
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I've defined a function similar to

check x assoc = let found = lookup x assoc in
                    when (isJust found) $ putStrLn $ "found " ++ fromJust found

which I've used several times from within the IO monad. Is there a more
compact way of doing this?




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

Message: 2
Date: Thu, 1 Aug 2013 18:09:11 +0300
From: Gesh hseG <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Shorten this code
Message-ID:
        <cacs5xqnk4qomoy--g6a8hnny2dm_7ganoy3rkazovf4b1zu...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Wed, Jul 24, 2013 at 4:18 PM, Henk-Jan van Tuyl <[email protected]> wrote:
> On Tue, 23 Jul 2013 12:41:32 +0200, Nadav Chernin <[email protected]>
> wrote:
>
>> Hi, all
>> Below is my solution to SPOJ->Polybius
>> square<http://www.spoj.com/problems/POLYBIUS>
>>
>>
>> Please try to shorten it:
>>
>> *import Data.Maybe*
>> *d=[1..5]*
>> *f s=unwords$map(\c->fromJust$lookup c(('
>> ',""):('J',"24"):zip(['A'..'I']++['K'..'Z'])[show(x+10*y)|y<-d,x<-d]))s*
>> *main=getLine>>(interact$unlines.map f.lines)*
> d="12345"
> f s = unwords $ map (\c -> let (Just x) = lookup c ((' ',"") : ('J',"24") :
> zip (['A'..'I'] ++ ['K'..'Z']) [y : [x] | y <- d, x <- d]) in x) s
> main = getLine >> (interact $ unlines . map f . lines)

Note that you can point-freeify that solution a little and sacrifice totality,
yielding an even shorter solution. (I didn't remove the lambda expression,
as it ends up being shorter than flipping lookup)
Inline the definitions and remove redundant spaces and newlines
in the code below:
d = "12345"
f = unwords . map $
\c -> fromJust $ lookup c ((' ',"") : ('J',"24") : zip
(['A'..'Z']\\"J") [[y,x] | y <- d, x <- d])
main = interact $ unlines . map f . drop 1 . lines

Just my two cents.
Gesh



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

Message: 3
Date: Thu, 1 Aug 2013 17:34:15 +0200
From: Krzysztof Skrz?tnicki <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] maybe in IO
Message-ID:
        <cam7aeve2-f7yyr89nf4cfvo+ic6d6ytojurboygmmxtle4d...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'd rather use pattern matching in this case:

check x assoc = case lookup x of
                    Just found -> putStrLn ("found " ++ found)
                    _ ->

Not really shorter but I think it's cleaner this way.




On Thu, Aug 1, 2013 at 5:00 PM, vold <[email protected]> wrote:

> I've defined a function similar to
>
> check x assoc = let found = lookup x assoc in
>                     when (isJust found) $ putStrLn $ "found " ++ fromJust
> found
>
> which I've used several times from within the IO monad. Is there a more
> compact way of doing this?
>
>
> _______________________________________________
> 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/20130801/68b73539/attachment-0001.html>

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

Message: 4
Date: Thu, 1 Aug 2013 17:35:22 +0200
From: Krzysztof Skrz?tnicki <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] maybe in IO
Message-ID:
        <cam7aevg+h6opfaprhrpd0je75nxpyurmccxx0b26fkqayq-...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Of course there is "return ()" missing.

check x assoc = case lookup x of
                    Just found -> putStrLn ("found " ++ found)
                     _ -> return ()


On Thu, Aug 1, 2013 at 5:34 PM, Krzysztof Skrz?tnicki <[email protected]>wrote:

> I'd rather use pattern matching in this case:
>
> check x assoc = case lookup x of
>                     Just found -> putStrLn ("found " ++ found)
>                     _ ->
>
> Not really shorter but I think it's cleaner this way.
>
>
>
>
> On Thu, Aug 1, 2013 at 5:00 PM, vold <[email protected]> wrote:
>
>> I've defined a function similar to
>>
>> check x assoc = let found = lookup x assoc in
>>                     when (isJust found) $ putStrLn $ "found " ++ fromJust
>> found
>>
>> which I've used several times from within the IO monad. Is there a more
>> compact way of doing this?
>>
>>
>> _______________________________________________
>> 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/20130801/b74b275a/attachment-0001.html>

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

Message: 5
Date: Thu, 1 Aug 2013 18:39:22 +0300
From: Gesh hseG <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] maybe in IO
Message-ID:
        <CACS5XqPV4x6Rk_J3zaW21wphbDFAkH+NvsxFdZm=qp_g3th...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Thu, Aug 1, 2013 at 6:00 PM, vold <[email protected]> wrote:
> I've defined a function similar to
>
> check x assoc = let found = lookup x assoc in
>                     when (isJust found) $ putStrLn $ "found " ++ fromJust 
> found
>
> which I've used several times from within the IO monad. Is there a more
> compact way of doing this?
Firstly, note that it is more idiomatic to move the IO code to other functions,
which would allow check to be pure.
Secondly, one can use the fact that Maybe is a Functor to rewrite check
as follows:
check x db = maybeToIO $ (putStrLn . ("found"++)) <$> x `lookup` db
  where maybeToIO = fromMaybe (return ())
The refactoring of maybeToIO isn't strictly necessary, but I found it clearer.
HTH,
Gesh



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

Message: 6
Date: Thu, 01 Aug 2013 11:45:02 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] maybe in IO
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 08/01/2013 11:00 AM, vold wrote:
> I've defined a function similar to
> 
> check x assoc = let found = lookup x assoc in
>                     when (isJust found) $ putStrLn $ "found " ++ fromJust 
> found
> 
> which I've used several times from within the IO monad. Is there a more
> compact way of doing this?
> 

maybe (return ()) (putStrLn . ("found " ++)) found

is the best I could do.




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

Message: 7
Date: Fri, 02 Aug 2013 00:18:14 +0200
From: "Henk-Jan van Tuyl" <[email protected]>
To: "The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell" <[email protected]>,
        "Gesh hseG" <[email protected]>
Subject: Re: [Haskell-beginners] Shorten this code
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
        delsp=yes

On Thu, 01 Aug 2013 17:09:11 +0200, Gesh hseG <[email protected]> wrote:

> On Wed, Jul 24, 2013 at 4:18 PM, Henk-Jan van Tuyl <[email protected]>  
> wrote:
>> d="12345"
>> f s = unwords $ map (\c -> let (Just x) = lookup c ((' ',"") :  
>> ('J',"24") :
>> zip (['A'..'I'] ++ ['K'..'Z']) [y : [x] | y <- d, x <- d]) in x) s
>> main = getLine >> (interact $ unlines . map f . lines)
>
> Note that you can point-freeify that solution a little and sacrifice  
> totality,
> yielding an even shorter solution. (I didn't remove the lambda  
> expression,
> as it ends up being shorter than flipping lookup)
> Inline the definitions and remove redundant spaces and newlines
> in the code below:
> d = "12345"
> f = unwords . map $
> \c -> fromJust $ lookup c ((' ',"") : ('J',"24") : zip
> (['A'..'Z']\\"J") [[y,x] | y <- d, x <- d])
> main = interact $ unlines . map f . drop 1 . lines

There are imports necessary for fromJust and \\, this makes it longer  
again.

Regards,
Henk-Jan van Tuyl


-- 
Folding@home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--



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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 62, Issue 1
****************************************

Reply via email to