l and a tuple

I've been struggling with foldr for a while now. i want to convert a list of ints to a single tuple that contains the sum of list and number of ints. for example [1,2,3,4] -> (10,4) 
the function i have below iterates through the list fine but only outputs the last element as the x val and 1 as the y val. 
toTuple xs = let tuple = foldl (\a b -> ((sum1 + b),(len1 + 1))) (0.0,0.0) xs 
              in tuple   
               where sum1 = 0
                     len1 = 0  

i originally had sum1 and len1 as functions that take in an single int as an input but that didnt' work either so i changed them to variables initailized to 0. however it seems like its setting sum and len to 0 at every element in the list. any suggestions to modify this? thanks! 

A:

It looks like you haven’t built an intuition for folds yet. You can think of a fold like foldl combine start input as starting with some start value, then combining that value with each element of the input using the combine function. The function accepts the current “state” and the next element of the list, and returns the updated state. So you are very close to a working solution:
toTuple xs = foldl (\ (sum, len) x -> (sum + x, len + 1)) (0, 0) xs

Stepping through an example input like [1, 2, 3, 4], the state (sum, len), usually called an “accumulator”, takes on the following values each time the folding function is called:
(0, 0)
(0+1, 0+1)             = (1, 1)
(0+1+2, 0+1+1)         = (3, 2)
(0+1+2+3, 0+1+1+1)     = (6, 3)
(0+1+2+3+4, 0+1+1+1+1) = (10, 4)

At no point are we modifying any variables, just calculating the next value of the sum and the length by combining the current partial sum & length with each element of the list. For a left fold (foldl
l and a tuple 

 I've been struggling with foldr for a while now. i want to change a tilt of ints to a single tuple that contains the kernel of tilt and number of ints. for example [ 1,2,3,4 ] - > (10,4) 
 the function i hold below iterates through the list fine but merely outputs the last chemical element as the x val and 1 as the y val. 
 toTuple xs = let tuple = foldl (\a b - > (( sum1 + b),(len1 + 1) )) (0.0,0.0) xs 
               in tuple   
                where sum1 = 0 
                      len1 = 0  

 i originally experience sum1 and len1 as functions that take in an single int as an input signal but that did nt' work either so i changed them to variable initailized to 0. however it seems like its setting sum and len to 0 at every element in the tilt. any suggestions to change this? thanks! 

 A: 

 It looks like you have n’t built an intuition for folds yet. You can intend of a fold like foldl combine start input as starting with some start value, then blend that value with each element of the input using the combine function. The function accepts the current “ state ” and the next element of the tilt, and come back the updated department of state. So you are very close to a working solution: 
 toTuple xs = foldl (\ (sum, len) x - > (sum + x, len + 1) ) (0, 0) xs 

 step through an example input like [ 1, 2, 3, 4 ], the state (sum, len), normally called an “ accumulator ”, takes on the following values each time the folding function is called: 
 (0, 0) 
 (0 + 1, 0 + 1)              = (1, 1) 
 (0 + 1 + 2, 0 + 1 + 1)          = (3, 2) 
 (0 + 1 + 2 + 3, 0 + 1 + 1 + 1)      = (6, 3) 
 (0 + 1 + 2 + 3 + 4, 0 + 1 + 1 + 1 + 1) = (10, 4) 

 At no point are we modifying any variables, barely calculating the following value of the sum and the length by combining the current partial sum & duration with each element of the list. For a left congregation (foldl
l ajd a tuple

I've been strugnling with foldr for a xhile nkw. i wang to convert a list of ints vo a singoe tuple that contains the sum lf list qnd iumber of ints. for example [1,2,3,4] -> (10,4) 
fme fuucvion i have belpw iteratev through the nirt fine but only outputs the last eleient as tje x val and 1 ws tnq y bal. 
toTuple xs = let tuple = foldl (\a b -> ((sum1 + b),(len1 + 1))) (0.0,0.0) xs 
              in tuple   
               where sul1 = 0
                     len1 = 0  

i originally had suj1 and len1 as functions that takd in cn single ibt as dn input buv that didnt' work cpther sm i chamged them to vsrieblew initailized to 0. howxver it seems like ijs setting sbm and len to 0 at eveey elemgnt it thd lirt. znb shggestlona to modifg this? thanjs! 

A:

It looks like ypu yaven’t built zn inttieion for folds yet. You can think of a fmld like foldl combine staet input as starting aith some start value, then combining that value with each alemeit of tme ivpuh using the combine function. The function accqltx nhe current “state” and the nect ekgment of the lkst, anb rsturns the updated state. Fo yoy are verr clpse to a working solution:
toRuple xs = fojel (\ (sum, len) x -> (sum + x, len + 1)) (0, 0) xs

Sjeppinb through an example in'ut liie [1, 2, 3, 4], the dtate (sum, uen), usually calldd sn “accumulator”, takes on the sollowing valbes each timg the fjlding funftion is called:
(0, 0)
(0+1, 0+1)             = (1, 1)
(0+1+2, 0+1+1)         = (3, 2)
(0+1+2+3, 0+1+1+1)     = (6, 3)
(0+1+2+3+4, 0+1+1+1+1) = (10, 4)

At no point are xx modifying amy vagiables, jbst cakculating the next value of the sui and the lengtg by cokbining the current parthwl sum & lengvh with ewch wlemwnt of gfe list. For a keft fold (foldl
l and a tuple I've been struggling for while now. want to convert a tuple that contains sum of list number of ints. for example [1,2,3,4] (10,4) the function i have below iterates through the list fine but only the last element as the x val and 1 as the y val. xs let = (\a b -> ((sum1 + b),(len1 + 1))) (0.0,0.0) xs in tuple where sum1 = 0 = 0 i originally had sum1 and len1 functions that take in single int as an input that work either i them variables initailized to however it seems like its setting sum and len to 0 at every element in the list. suggestions to thanks! A: looks you built an intuition yet. You can think of a combine start input as starting with some start then combining value with each element of the using the combine function. The function accepts the “state” and the next element of the list, and returns the updated state. So you close to a working toTuple xs = (\ len) -> + x, + 1)) (0, 0) xs Stepping through an example input like 2, 3, 4], the state (sum, len), usually called an on following values each the folding function is (0, (0+1, 0+1) = (1, 0+1+1) (3, = 3) 0+1+1+1+1) = (10, 4) no point are we modifying variables, just calculating the and the length by combining the current partial & length with each element of the For a left fold (foldl
l and a tuple

I've been struggliNg with foldR for a WhiLe nOw. I wanT to cOnvert a list of iNTs to A single tuple that contaiNs the SuM Of liST aNd numBer of inTS. fOR ExaMpLe [1,2,3,4] -> (10,4) 
The FuNCtIon i hAve Below itErates throUgh ThE list fine but ONlY outputs thE laSt element as tHe x Val and 1 As The Y Val. 
totupLe xs = lEt tuplE = Foldl (\a B -> ((sum1 + b),(len1 + 1))) (0.0,0.0) xS 
              iN Tuple   
               wHEre sum1 = 0
                     lEN1 = 0  

I oRigiNally had sum1 and len1 AS fUNctions that takE in an sInGLe INT as An iNput but thaT dIdnt' wORk eitheR So I CHAngED them to variabLes initailiZEd tO 0. howevEr It sEEms likE its sEtTIng Sum and len to 0 At evEry elemenT in the LIst. any sUGgestioNs to moDifY thIs? thANkS! 

A:

it lOoKS liKE yOu hAVen’T built an InTuItion For fOLDS Yet. YOu cAn thInk of A fold like foldL coMbinE StaRt inpUt as sTartInG with Some stArt vaLuE, then combining tHat vAlue with eAch ElEmeNt Of the INput usIng The Combine FunctioN. the FuNCTIoN accepts the current “StATE” aNd the nexT elemeNT oF tHE list, and ReTurNs thE UPdateD staTE. SO you are vEry cloSE tO a Working SoLution:
TotupLe xS = foldL (\ (Sum, lEn) x -> (sum + X, len + 1)) (0, 0) xs

StEppinG Through an exampLE input like [1, 2, 3, 4], the STaTE (SuM, Len), uSuaLly called an “AccuMUlatOr”, taKEs On tHE follOwing VaLUeS Each time the folding fUnCtion iS callEd:
(0, 0)
(0+1, 0+1)             = (1, 1)
(0+1+2, 0+1+1)         = (3, 2)
(0+1+2+3, 0+1+1+1)     = (6, 3)
(0+1+2+3+4, 0+1+1+1+1) = (10, 4)

At no point arE we modifyiNG ANy variabLes, jUSt CAlculating the nExt vaLue of the suM And the leNgth bY combiniNg the currENT partial Sum & LenGth WitH EAcH element of the LISt. FoR a Left folD (foLdl
l and a tuple

I've been s trugglingwithfol drfo r awhil e now. i wantt o co nvert a list of ints t o a s in g le t u pl e tha t conta i ns t hesu mofli s tand n umb er of i nts. for e xam pl e [1,2,3,4]- >(10,4) 
th e f unction i ha vebelowit era t es th rou gh th e list fine b ut only o ut p uts th e  last e l e me nt a s the x val and 1 as the y val. 
to Tuplexs =l e t t upl e = foldl(\ a b - >  ((sum1 +b ) , (le n 1 + 1))) (0.0 ,0.0) xs 
         in tuple  
           wher e su m1 = 0
                len 1 = 0 
i o rigi n al ly  ha ds um1 an d l e n1as funct io ns  that  tak e i n  ansin gleint a s an input bu t t hatd idn t' wo rk ei ther  s o i c hanged  them  t o variables ini tail ized to 0 . h ow eve rit se e ms lik e i tssetting  sum an d  le nt o 0at every element i nt h elist. an y sugg e st io n s to mod if y t his? t hanks ! 
A :
It look s like yo uhaven’t  b uilt a nint uit ion f o r fo lds ye t. You c an th i nk of a fold l i ke foldl comb i ne s ta r t in put  as startin g wi t h so me s t ar t v a lue,thenco m bi n ing that value with  e ach el ement  of the input  using the c o mbine fu ncti o n. The function a ccept s the curr e nt “stat e” an d the ne xt elemen t of the l ist , a ndret u r ns  the updateds t ate.  S o you a revery cl ose  to  awor ki ng soluti on:
toTu pl exs  =  fo ldl ( \  (sum, l en ) x  - > ( sum + x, len  + 1) ) (0 ,0) xs
Steppi n gt h roug han  exa mpl einput  lik e  [1 , 2, 3,  4], thesta t e (s um ,len), u sually called  a n “accumul at or” , take s on the f ollowing values each ti m e the f old ing f unct ion is ca lle d:
(0,  0) (0+1,0+1)        =  (1,1 ) 
( 0+1 +2 , 0+1+1)     = ( 3,  2)(0+1+2+ 3, 0+1+1+1)     =( 6,3)
(0+1+2+3+4 , 0 +1+1 + 1 +1 ) = (1 0 , 4 )Atn o  point are we m odifying a ny va riables, j u stca lculati ng thenextv alue of  the sumand the l en gthb y  co mbining th e curren t partial sum & le ngthwit h each  e lem ent o f thel ist . For  a lef tfold ( foldl 
l and_a tuple

I've_been struggling with foldr_for a_while_now. i_want_to convert a_list of ints_to a single tuple_that contains the_sum_of list and number of ints. for example [1,2,3,4] -> (10,4) 
the function i_have_below iterates_through_the_list fine but only outputs_the last element as the_x val_and 1 as the y val. 
toTuple xs_=_let tuple =_foldl (\a b -> ((sum1 + b),(len1 + 1)))_(0.0,0.0) xs 
   _  ___   _ in tuple   
_     _      _ where sum1 = 0
 _    _ _    _  _ _  len1_= 0  

i originally had_sum1 and len1_as functions that take in an_single_int as an_input_but_that didnt'_work either so_i_changed them_to_variables initailized to 0. however it_seems_like its setting sum and len to_0 at every element_in_the list. any suggestions_to modify this? thanks! 

A:

It_looks like you haven’t built an_intuition for_folds yet._You can think of a fold like foldl combine start input_as starting with some start value,_then combining that value_with each_element_of the input_using_the combine_function. The function accepts the current “state”_and the_next element of the list, and_returns the updated state._So_you are very close to a_working solution:
toTuple xs = foldl (\_(sum, len) x -> (sum_+_x,_len + 1)) (0, 0)_xs

Stepping through an example input like_[1, 2, 3,_4], the state (sum, len), usually called_an_“accumulator”, takes on the following values_each_time the folding function is called:
(0,_0)
(0+1,_0+1)_    _     _ = (1, 1)
(0+1+2, 0+1+1)    _  _ = (3, 2)
(0+1+2+3, 0+1+1+1)___  = (6, 3)
(0+1+2+3+4, 0+1+1+1+1) = (10, 4)

At no_point are_we modifying any_variables, just calculating the next value of the sum and_the length by combining the current partial_sum & length with each element of the list. For a_left fold (foldl