Re: Beginners Digest, Vol 122, Issue 19

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Beginners Digest, Vol 122, Issue 19

Andrey Klaus
Hello everybody,

I do not have many experience with haskell, but I'm programming for more then 10 years
and I'm spending almost all my free time to learn FP during last 1.5 years.

I have read book being discussed (Graham Hutton) and I would like to say just a couple words about it.

First of all you need remember that it is book for beginners, so, IMO author would prefer
to use "possible easy to understand" way instead of "most type safe" or "most canonical".
And this is good for beginners because otherwise details take too many time with
unreasanable low results.

Hutton's style is very understandable. No even one needless detail over all the book.
Code looks to me simple, concise and very clean. Despite it looks not safe
sometimes, it is always perfectly reasonable. I want to say I would be happy
if I were to write such code in production.

When one has his own experience in haskell, she or he will be able to choose his own style.
But for beginners I would surely recommend to follow Hutton's style.

wbr,
Andrey

пн, 27 авг. 2018 г. в 15:41, <[hidden email]>:
Send Beginners mailing list submissions to
        [hidden email]

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
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Hutton ex 7.7 and 7.8 (C Maeder)


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

Message: 1
Date: Mon, 27 Aug 2018 10:02:48 +0200
From: C Maeder <[hidden email]>
To: [hidden email]
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[hidden email]>
Subject: Re: [Haskell-beginners] Hutton ex 7.7 and 7.8
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=utf-8

Hi, see my comments below

Am 24.08.2018 um 11:45 schrieb trent shipley:
>
> I am mostly looking for style feedback, although if there are any
> obvious logic errors, I'd be "happy" to learn about those too.

[...] let me ignore the exercise text

>
> type Bit = Int

A bit is not an Int! A user-defined type (or Bool) would be type safer
(although arithmetic is missing).

>
> byte :: Int
> byte = 8
>
> parityByte :: Int
> parityByte = 9
>
> bin2int :: [Bit] -> Int 
> bin2int = foldr (\x y -> x + 2 * y) 0

use here "bit2int x" if bits are no ints.

>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2647-2649).
> Cambridge University Press. Kindle Edition. 
>
> int2bin :: Int -> [Bit] 
> int2bin 0 = [] 
> int2bin n = n `mod` 2 : int2bin (n `div` 2)

here you could use "even n" if bits are Bool.

>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2654-2656).
> Cambridge University Press. Kindle Edition. 
>
> make8 :: [Bit] -> [Bit] 
> make8 bits = addParity (take byte (bits ++ repeat 0))
>
> -- Parity functions
>
> addParity :: [Bit] -> [Bit]
> addParity xs = if even (sum xs) 
>                then xs ++ [0]
>                else xs ++ [1]

appending the parity (at the end of a list) is inefficient. The parity
could be the first bit if it is not stored separately.

>                
> checkParity :: [Bit] -> Bool
> checkParity xs = (((even . sum) (take ((length xs) - 1) xs)) == 
>                      ((even . last) xs)) ||   
>                  (((odd  . sum) (take ((length xs) - 1) xs)) == 
>                      ((odd  . last) xs))

here is too much duplicate code! You could also use the function "init",
if the input list xs is (checked to be) non-empty.

>                  
> errorParity :: [Bit] -> ([Bit], Bool)
> errorParity xs = if checkParity xs 
>                  then (xs, checkParity xs) 
>                  else error "Parity error"

the result of this function is nonsense. The input (usually) does not
need to be returned and the boolean result can only be true, since the
other case fails with a runtime error.

>                  
> dropParity :: [Bit] -> [Bit]
> dropParity xs = take ((length xs) - 1) xs

this function could have been reused in checkParity.

>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2662-2663).
> Cambridge University Press. Kindle Edition.
>
> -- TRANSMISSION
>
> encode :: String -> [Bit] 
> encode = concat . map (make8 . int2bin . ord)
>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2673-2675).
> Cambridge University Press. Kindle Edition. 
>
> chop8 :: [Bit] -> [[Bit]] 
> chop8 [] = [] 
> chop8 bits = (dropParity . fst . errorParity) (take parityByte bits) : 
>                  chop8 (drop parityByte bits)

use "splitAt" instead of take and drop. (It may not be very nice to fail
with a runtime error by using errorParity.)

>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2681-2683).
> Cambridge University Press. Kindle Edition. 
>
> decode :: [Bit] -> String 
> decode = map (chr . bin2int) . chop8
>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2686-2688).
> Cambridge University Press. Kindle Edition. 
>
> -- channel :: [Bit] -> [Bit] 
> -- channel = id
>
> channel :: [Bit] -> [Bit] 
> channel = tail

"tail" is partial! This should be documented (if this is ok). Did you
try to transmit an empty string?

Cheers Christian

>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2696-2697).
> Cambridge University Press. Kindle Edition. 
>
> transmit :: String -> String 
> transmit = decode . channel . encode
>
> -- Hutton, Graham. Programming in Haskell (Kindle Locations 2694-2695).
> Cambridge University Press. Kindle Edition. 
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 122, Issue 19
******************************************

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners