Posted By

rtperson on 06/01/11

Real World Haskell Exercise - Ch 4, Ex 2-4 `import Data.Char (digitToInt) -- we'll need ordimport Data.List (foldl')    {- Ex 2: The asInt_fold function uses error, so its          callers cannot handle errors. Rewrite it to          fix this problem.-}type ErrorMessage = StringasInt_either :: String -> Either ErrorMessage IntasInt_either []  = Left "String is empty"asInt_either "-" = Left "dash is not a number"asInt_either ('-':xs) = Right \$ (-1) * asInt_fold xs asInt_either str = Right \$ asInt_fold str {- Ex 3: The Prelude function concat          concatenates a list of lists into a single list,          and has the following type.         concat :: [[a]] -> [a]                Write your own definition of concat using foldr.-} list1 = [1,2,3,4,5]list2 = [6,7,8,9,10]list3 = [11,12,13,14,15]list4 = [16,17,18,19,20]listA = [list1, list2, list3, list4] -- this was easy...concat_foldr :: [[a]] -> [a]concat_foldr = foldr (++) [] {- Ex 4: Write your own definition of the standard takeWhile          function, first using explicit recursion, then foldr.                  (I assume 2 separate functions...)                    --- test cases (from the Prelude comments)          takeWhile (< 3) [1,2,3,4,1,2,3,4] == [1,2]          takeWhile (< 9) [1,2,3] == [1,2,3]          takeWhile (< 0) [1,2,3] == [] -} takeWhile_rec :: (a -> Bool) -> [a] -> [a]takeWhile_rec _ []          =  []takeWhile_rec p (x:xs)                | p x       = x : takeWhile_rec p xs                | otherwise = [] takeWhile_foldr :: (a -> Bool) -> [a] -> [a]takeWhile_foldr p = foldr (\x xs -> if p x then x:xs else []) [] dropWhile_foldr :: (a -> Bool) -> [a] -> [a]dropWhile_foldr p = foldr (\x xs -> if p x then xs else x:xs) []`
{"ft_lang_label":"__label__en","ft_lang_prob":0.7701358,"math_prob":0.99117607,"size":1877,"snap":"2019-26-2019-30","text_gpt3_token_len":628,"char_repetition_ratio":0.13828084,"word_repetition_ratio":0.08355795,"special_character_ratio":0.4176878,"punctuation_ratio":0.20775624,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9924679,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-06-26T12:29:19Z\",\"WARC-Record-ID\":\"<urn:uuid:5a215615-265c-4f1f-92c4-1ba6c3683345>\",\"Content-Length\":\"27070\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e5d76d54-6022-4488-a892-a942078c08af>\",\"WARC-Concurrent-To\":\"<urn:uuid:da0dd840-f4cf-45a5-a4fe-9722dff4d553>\",\"WARC-IP-Address\":\"104.24.31.48\",\"WARC-Target-URI\":\"https://snipplr.com/view/54659/real-world-haskell-exercise--ch-4-ex-24/\",\"WARC-Payload-Digest\":\"sha1:B3CG6DURXXYRP2F2TCWJFZRVGXIJRDQV\",\"WARC-Block-Digest\":\"sha1:5IYAZZ7V4NX7XIZO2OTJIJGLPEQWXLD6\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-26/CC-MAIN-2019-26_segments_1560628000306.84_warc_CC-MAIN-20190626114215-20190626140215-00495.warc.gz\"}"}