Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/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: how can I make this work (Roelof Wobben)
2. Are these solution the Haskell way ? (Roelof Wobben)
3. Re: Are these solution the Haskell way ? (Daniel P. Wright)
----------------------------------------------------------------------
Message: 1
Date: Tue, 12 May 2015 21:14:59 +0200
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] how can I make this work
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150512/56e5126e/attachment-0001.html>
------------------------------
Message: 2
Date: Wed, 13 May 2015 08:10:17 +0200
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Are these solution the Haskell way ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
Hello,
For practising pattern matching and recursion I did recreate some
commands of Data,list.
My re-implementation of ++ :
plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx
plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)
main = print $ plusplus ["a","b"] ["c","d"]
my re-implementation of init :
import Data.Maybe
-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))
main = print . init' $ [1,3]
my re-implementation of last :
-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs
main = print . last' $ []
Now I wonder if these solutions are the haskell way ? if not so, how can
I improve them ,
Roelof
---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com
------------------------------
Message: 3
Date: Wed, 13 May 2015 15:27:23 +0900
From: "Daniel P. Wright" <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Are these solution the Haskell way ?
Message-ID:
<CAP=piQcz0h=NoOnTEf_CQeUsurytojCSj7LE7R4Hd=cbvh9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi Roelof,
If you don't consider it cheating (and I suggest you shouldn't, having had
a stab at the answers), you will find great enlightenment looking at how
these functions are *actually* implemented in the wild. Did you know that
there is a "source" link for each function on Hackage? By clicking on
that, for your first example, you can compare the actual implementation of
(++) with your "plusplus" function:
http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#%2B%2B
(By the way, it's that function in particular that gave me one of my first
"Aha!" moments in Haskell... it's quite beautiful in its simplicity).
One thing with viewing the source on Hackage is that sometimes it can be a
little more confusing than it needs to be for the sake of efficiency. A
really good source for good, readable examples of Prelude functions in
Haskell is the Haskell Report:
https://www.haskell.org/onlinereport/standard-prelude.html
(In this case, though, the implementation is the same).
Having a shot at defining these library functions yourself, as you have
done, and then comparing your version with the "official" version in the
prelude is a great way to learn good style!
-Dani.
2015-05-13 15:10 GMT+09:00 Roelof Wobben <[email protected]>:
> Hello,
>
> For practising pattern matching and recursion I did recreate some commands
> of Data,list.
>
> My re-implementation of ++ :
>
> plusplus :: [a] -> [a] -> [a]
> plusplus [] [] = [] ;
> plusplus [] (xs) = xs
> plusplus (xs) [] = xs
> plusplus (xs) yx = plusplus' (reverse xs) yx
>
> plusplus' :: [a] -> [a] -> [a]
> plusplus' [] (xs) = xs
> plusplus' (x:xs) yx = plusplus' xs (x:yx)
>
> main = print $ plusplus ["a","b"] ["c","d"]
>
> my re-implementation of init :
>
> import Data.Maybe
>
> -- | The main entry point.
> init' :: [a] -> Maybe [a]
> init' [] = Nothing
> init' [x] = Just []
> init' (x:xs) = Just (x:fromMaybe xs (init' xs))
>
> main = print . init' $ [1,3]
>
>
> my re-implementation of last :
>
> -- | The main entry point.
> last' :: [a] -> Maybe a
> last' [] = Nothing
> last' [x] = Just x
> last' (_:xs) = last' xs
>
> main = print . last' $ []
>
> Now I wonder if these solutions are the haskell way ? if not so, how can I
> improve them ,
>
> Roelof
>
>
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> http://www.avast.com
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150513/f86bf925/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 83, Issue 25
*****************************************