haskell - is mult_add a real function? What does it do? -
i have following question given me.
write function form_number_back takes list of positive integers , forms decimal number using numbers in list in reverse order.
for example form_number_back [1, 2, 3, 4] should return number 4321; form_number_back [ ] returns 0
use function foldr , mult_add below accomplish
mult_add d s = d + 10*snote: foldr , foldr1 2 different functions. try use foldr1 instead of foldr in definition , see if same results empty list. explain results.
i cannot find on mult_add. thought mabye function name wants form_number_back function name. means mult_add haskell function.
can explain me mult_add does? written right? mult_add usermade function i'm supposed use own code?
edit 2
i tried putting in function example type.. so.. form_number_back [1, 2, 3, 4] :: num b => b -> [b] -> b
so function looks like
form_number_back = foldr(mult_add) but returning type of
form_number_back :: num b => [t] -> b -> [b] -> b trying figure out how rid of [t]
types both more important , more informative in haskell in other languages. when don't understand haskell, first step think types. let's that. we'll fire ghci , enter:
prelude> let mult_add d s = d + 10 * s and ask type:
prelude> :t mult_add mult_add :: num => -> -> that is, mult_add takes a , a, , returns a, proviso a instance of num class (so can add , multiply them).
you're asked use foldr write function, let's @ type of that:
prelude> :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b that looks bit intimidating, let's break down. first part, (a -> b -> b) tells foldr needs function of 2 variables, a , b. well, have 1 of - it's mult_add. happens if feed in mult_add first argument foldr?
prelude> :t foldr mult_add foldr mult_add :: num b => b -> [b] -> b okay! have function takes b , [b] (a list of bs) , returns b. function you're trying write needs return 0 when it's given empty list, let's try feeding empty list, few different values remaining argument:
prelude> foldr mult_add 10 [] 10 prelude> foldr mult_add 5 [] 5 hey, that's interesting. if feed number x , empty list, returns x (note: true foldr. if give initial value x , empty list [], return x, no matter function use in place of mult_add.)
so let's try feeding 0 second argument:
prelude> foldr mult_add 0 [] 0 that seems work. how if feed list [1,2,3,4] instead of empty list?
prelude> foldr mult_add 0 [1,2,3,4] 4321 nice! seems work. question is, why work? trick understanding foldr foldr f x xs inserts function f between every element of xs, , additionally puts x @ end of list, , collects right (that's why it's called right fold). so, example:
foldr f 0 [1,2,3] = 1 `f` (2 `f` (3 `f` 0)) where backticks indicate we're using function in infix form (so first argument 1 on left, , second argument 1 on right). in example have f = mult_add, multiplies second argument 10 , adds first argument:
d `mult_add` s = d + 10 * s so have
foldr mult_add 0 [1,2,3] = 1 `mult_add` (2 `mult_add` (3 `mult_add 0)) = 1 `mult_add` (2 `mult_add` 3) = 1 `mult_add` 32 = 321 which expect. make sure understand this, work out happen if defined mult_add other way around, i.e.
mult_add d s = 10 * d + s
Comments
Post a Comment