fold

Getting average of calculated list haskell [duplicate]

℡╲_俬逩灬. 提交于 2019-12-11 13:51:12
问题 This question already has answers here : Outputting the contents of a list of a custom data type (2 answers) Closed 5 years ago . I have avgRatingsForDirector :: String -> [Film] -> [Int] avgRatingsForDirector _ [] = [] avgRatingsForDirector requestedDirector ((Film _ director _ ((_, rating):ratings)):restOfFilms) | requestedDirector == director = [rating] ++ avgRatingsForDirector requestedDirector restOfFilms | otherwise = avgRatingsForDirector requestedDirector restOfFilms This outputs a

Gap function that returns the integer distance between first appearance of two elements in a list using either foldl or foldr.(Haskell)

不羁的心 提交于 2019-12-11 13:17:14
问题 the type is defined as follows: gap :: (Eq a) => a -> a -> [a] -> Maybe Int I have been stuck on this problem for more than an hour and have no idea how to approach the problem. I am aware that it requires the use of fold and am familiar with that topic. Please take into consideration that either foldl or foldr must be used. The output when called ought to look like this gap 3 8 [1..10] =Just 5 gap 8 3 [1..10] =Nothing gap 'h' 'l' "hello" =Just 2 gap 'h' 'z' "hello" =Nothing 回答1: You might

Haskell foldr results in type error while foldl doesn't

不羁岁月 提交于 2019-12-11 12:10:26
问题 I'm working through "Haskell Programming From First Principles". In the chapter on Folding Lists, exercise 5f, when I evaluate foldr const 'a' [1..5] I get No instance for (Num Char) arising from the literal ‘1’ However, with foldl const 'a' [1..5] I get 'a' . I get that the folds are lazy, foldr doesn't traverse the spine and foldl does. But even looking at the definitions of foldr and foldl, foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) foldl f z [] = z foldl f z (x:xs) = foldl f

Haskell - Removing adjacent duplicates from a list

こ雲淡風輕ζ 提交于 2019-12-11 04:33:48
问题 I'm trying to learn haskell by solving some online problems and training exercises. Right now I'm trying to make a function that'd remove adjacent duplicates from a list. Sample Input "acvvca" "1456776541" "abbac" "aabaabckllm" Expected Output "" "" "c" "ckm" My first though was to make a function that'd simply remove first instance of adjacent duplicates and restore the list. module Test where removeAdjDups :: (Eq a) => [a] -> [a] removeAdjDups [] = [] removeAdjDups [x] = [x] removeAdjDups

Concatenating strings with foldr in SML

痴心易碎 提交于 2019-12-11 03:03:54
问题 I'm trying to declare a function, string list -> string, that with the input for instance ["Chicago","city","USA"] should return "Chicago city USA" . What I did so far was this: fun gather ts = foldr op ^ "" ts; This seems to be somewhat along the lines, however the problem is, I would like to include the spaces between the words, as this function would return "ChigagocityUSA" . 回答1: Yes, the problem is that ^ is a function that for two strings "foo" and "bar" returns "foobar", although you

Correct use of a fold or reduce function to long-to-wide data in python or javascript?

为君一笑 提交于 2019-12-10 22:31:45
问题 Trying to learn to think like a functional programmer a little more---I'd like to transform a data set with what I think is either a fold or a reduce operation. In R, I would think of this as a reshape operation, but I'm not sure how to translate that thinking. My data is a json string that looks like this: s = '[ {"query":"Q1", "detail" : "cool", "rank":1,"url":"awesome1"}, {"query":"Q1", "detail" : "cool", "rank":2,"url":"awesome2"}, {"query":"Q1", "detail" : "cool", "rank":3,"url":

foldr and foldr1 Haskell

十年热恋 提交于 2019-12-10 21:01:02
问题 I am trying to dive deep in the folds, considering it seems a very powerful asset to me. However, can you help me with this: foldr (/) 2 [1,2,3] -- (1/(2/(3/2))), result 0,75 {where 2 is base) foldr1 (/) [2,2,3] -- (1/(2/(3/2))), result 3.00 {where 2 is base) I think I am seriously overseeing an essential difference between the folds. Thx 回答1: foldr :: (a -> b -> b) -> b -> [a] -> b has as implementation: foldr :: (a -> b -> b) -> b -> [a] -> b foldr _ z [] = z foldr f z (x:xs) = f x (foldr f

Fold function in Octave

荒凉一梦 提交于 2019-12-10 19:27:55
问题 Is there standard implementation of fold (reduce, aggregate etc) for one dimensional vector in Octave? If no, is there any way to express fold without using a loop statement? 回答1: The miscellaneous package provides the function reduce. For example, octave:6> reduce(@(x,y)(x*y), [1:5]) ans = 120 If you look at the source code for reduce , you'll see that it is a fairly simple Octave function that is implemented with a for loop, so it won't be more efficient than implementing the reduction with

Fold for Binary Tree

匆匆过客 提交于 2019-12-10 19:12:32
问题 I have to make an implementation of a Binary Tree instantiate a typeclass: class Set s where add :: (Eq a) => a -> s a -> s a remove :: (Eq a) => a -> s a -> s a exists :: (Eq a) => a -> s a -> Bool fold :: (a -> b -> b) -> s a -> b -> b data BTree k v = Empty | Node k v (BTree k v) (BTree k v) deriving (Show) All went well until I had to implement a fold for a binary tree. The issue I'm having is that I don't really know how to keep the type declaration of my function with a signature like

Take elements on even positions from the list

时间秒杀一切 提交于 2019-12-10 18:37:28
问题 Problem: using fold , take from the list elements which are on the even positions: GHCi> evenOnly [1..10] [2,4,6,8,10] GHCi> evenOnly ['a'..'z'] "bdfhjlnprtvxz" evenOnly :: [a] -> [a] evenOnly = undefined I decided at first to get a list of alternating 0 -es and 1 -s: [0,1,0,1..] Prelude> let g = iterate (\x -> (x + 1) `mod` 2) 0 Prelude> take 10 $ g [0,1,0,1,0,1,0,1,0,1] Then zip it with the original list, getting a list of pairs: [(x1, 0), (x2,1), (x3,0) .. (xn, ?)] : Prelude> zip g [1,2,3