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: Longest common prefix of a list of lists (James Cook)
2. is there a better way to ask for a hex formatted number?
(Sean Perry)
3. Re: is there a better way to ask for a hex formatted number?
(Daniel Fischer)
4. Re: Longest common prefix of a list of lists (aditya siram)
5. Re: Longest common prefix of a list of lists (Christopher Done)
----------------------------------------------------------------------
Message: 1
Date: Fri, 29 Apr 2011 15:42:17 -0400
From: James Cook <[email protected]>
Subject: Re: [Haskell-beginners] Longest common prefix of a list of
lists
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
On Apr 29, 2011, at 10:32 AM, Daniel Fischer wrote:
> -- to get the common prefix of two lists, we use explicit recursion,
> I
> don't see an elegant way to avoid that.
>
> commonPrefix :: Eq a => [a] -> [a] -> [a]
> commonPrefix (x:xs) (y:ys)
> | x == y = x : commonPrefix xs ys
> commonPrefix _ _ = []
>
> produces the result incrementally.
Personally, that's the definition I'd prefer. It's simple, clear, and
fast. If I were forced to hide the recursion, I would probably write
this:
> commonPrefix a b = map fst (takeWhile (uncurry (==)) (zip a b))
modulo usage of whatever combination of (.) and/or ($) is in style
today.
-- James
------------------------------
Message: 2
Date: Fri, 29 Apr 2011 14:11:01 -0700
From: Sean Perry <[email protected]>
Subject: [Haskell-beginners] is there a better way to ask for a hex
formatted number?
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I am using this currently:
makeHex n = "0x" ++ map toUpper (showHex n "") -- gives me 0xFF for example
I know i can use printf but I am not sending this to output when I am doing the
formatting.
------------------------------
Message: 3
Date: Fri, 29 Apr 2011 23:23:30 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] is there a better way to ask for a
hex formatted number?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-1"
On Friday 29 April 2011 23:11:01, Sean Perry wrote:
> I am using this currently:
>
> makeHex n = "0x" ++ map toUpper (showHex n "") -- gives me 0xFF for
> example
>
> I know i can use printf but I am not sending this to output when I am
> doing the formatting.
>
printf can also produce Strings:
Prelude Text.Printf> printf "0x%x" 239 :: String
"0xef"
Prelude Text.Printf> printf "0x%X" 239 :: String
"0xEF"
------------------------------
Message: 4
Date: Fri, 29 Apr 2011 18:10:33 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Longest common prefix of a list of
lists
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
-- Below is a solution that doesn't use length.
-- Documentation for each function and trial runs
-- are shown in a comment block at the end.
import Data.List.Ordered(isSortedBy)
import Safe(headMay)
import Maybe(fromJust)
a = "ababaaaa"
b = "ababcccc"
c = "ab"
d = ""
safeTail :: [a] -> [a]
safeTail [] = []
safeTail xs = tail xs
maybeTranspose :: [[a]] -> [[Maybe a]]
maybeTranspose [] = []
maybeTranspose xs | all null xs = []
| otherwise = map headMay xs : maybeTranspose (map
safeTail xs)
commonPrefix :: Eq a => [[a]] -> [a]
commonPrefix = map fromJust .
fmap (head . snd) .
takeWhile fst .
fmap (\a -> ((isSortedBy (==) a), a)) .
maybeTranspose
{-
Trial runs:
> commonPrefix [a,b] => "abab"
> commonPrefix [a,b,c] => "ab"
> commonPrefix [a,b,c,d] => ""
Functions:
'safeTail' only returns the tail of the list of a non-empty list
or an empty list
'maybeTranspose' is just like List.transpose except it
returns the biggest matrix, not the smallest
eg. transpose [[1,2],[],[4,5]] =>
[[1,4],[2,5]]
maybeTranspose [[1,2][][4,5]] =>
[[Just 1,Nothing,Just 4],
[Just 2,Nothing,Just 5],
[Nothing,Nothing,Nothing]]
'commonPrefix' transposes the input lists , extracts
the first n lists in the transposition where all the elements
are equal, extracts the element from each and returns a concatenation
of them.
eg. Given input ["ab", "abab", "aba"]
1. Transpose
[[Just 'a',Just 'a',Just 'a'],
[Just 'b',Just 'b',Just 'b'],
[Nothing,Just 'a',Just 'a'],
[Nothing,Just 'b',Nothing]]
2. Tag lists with equal elements
[(True,[Just 'a',Just 'a',Just 'a']),
(True,[Just 'b',Just 'b',Just 'b']),
(False,[Nothing,Just 'a',Just 'a']),
(False,[Nothing,Just 'b',Nothing])]
3. Take the first n lists with equal elements
[(True,[Just 'a',Just 'a',Just 'a']),
(True,[Just 'b',Just 'b',Just 'b'])]
4. Grab the equal element from each
[Just 'a',Just 'b']
5. Concatenate
"ab"
I didn't like using 'fromJust' but I don't see how it
can throw an exception in this case.
Working with the (Bool, [Maybe a]) tuples directly could
probably have been avoided by using Arrows, but it wasn't
obvious to me.
-}
------------------------------
Message: 5
Date: Sat, 30 Apr 2011 04:54:59 +0200
From: Christopher Done <[email protected]>
Subject: Re: [Haskell-beginners] Longest common prefix of a list of
lists
To: aditya siram <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On 30 April 2011 01:10, aditya siram <[email protected]> wrote:
>
> I didn't like using 'fromJust' but I don't see how it
> can throw an exception in this case.
>
?> catMaybes [Just 1,Nothing,Just 2]
[1,2]
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110430/afef2f3f/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 34, Issue 43
*****************************************