F# sum of a list of lists -
i created own datatype , i'm trying create sum of numbers in datatype list of lists. don't want use f# libraries
my datatype
type elist = | l of int * elist
i'm beginner in f# , trying grasp head on it. want recursively. thinking going traverse end of list , start sum , go front , add each one.
example:
let l = l(4, l(3, l(6, l(3, a))))
that should return
val : int 16
here's code , know wrong:
let rec sum l = let = 0 match l | -> 0 | l(head,a) -> head | l(head,tail) -> sum tail +
you're there. need lose a
:
let rec sum l = match l | -> 0 | l(head,a) -> head | l(head,tail) -> head + sum tail
then evaluating sum l
when l = l(4, l(3, l(6, l(3, a))))
gives
val : int = 16
as required.
Comments
Post a Comment