Change the function parameter only once.

I started with Haskell a few days ago. I have the following code snippet (recursive function):

totaltime t [x] = 0
totaltime t route = tbd + totaltime (t+tbd) (tail(route))
    where tbd = timebetweendepartures (fst(route!!0)) (fst(route!!1)) t (snd(route!!1))

"route" is a list of tuples, for example: [(3,1), (1,2), (5,2), (2,1)]

What I'm trying to do is to add (0,0) to the route before the function even starts doing what it should do.

So, if the parameter path is passed as [(3,1), (1,2), (5,2), (2,1)], then the function should get it as [(0,0), (3,1 ), (1,2), (5,2), (2,1)]

I could just do totaltime t ((0,0):route), but since the function is recursive, this will not work as intended.

Ideas are greatly appreciated!

+4
source share
2 answers

: totaltime where , (0,0):

totaltime t l = tt t ((0,0):l)
where tt t [x] = 0
      tt t route = tbd + tt (t+tbd) (tail(route))
          where tbd = timebetweendepartures (fst(route!!0)) (fst(route!!1)) t (snd(route!!1))

, , :

  • ;
  • , , , !!0 !!1, .

, , fst snd. , . , tt :

tt t [] = ... # create a case for the empty list
tt t [x] = 0
tt t ((x0,_):xys@((x1,y1):_)) = tbd + tt (t+tbd) xys
    where tbd = timebetweendepartures x0 x1 t y1

:

totaltime t l = tt t ((0,0):l)
    where tt t [] = ... # create a case for the empty list
          tt t [x] = 0
          tt t ((x0,_):xys@((x1,y1):_)) = tbd + tt (t+tbd) xys
              where tbd = timebetweendepartures x0 x1 t y1

, , totaltime prepends (0,0) , , , , .

, : t; , , :

totaltime t = tt t (0,0)
    where tt _ _ [] = 0
          tt t (x0,_) (xyh@(x1,y1):xyt) = tbd + tt (t+tbd) xyh xyt
              where tbd = timebetweendepartures x0 x1 t y1

, , , ( .

. , ( y0). :

totaltime t = tt t 0
    where tt _ _ [] = 0
          tt t x0 ((x1,y1):xyt) = tbd + tt (t+tbd) x1 xyt
              where tbd = timebetweendepartures x0 x1 t y1

, , .

+7

" ", :

 totaltime t route =
   totaltime' t ((0,0):route)
   where
      totaltime' t [x] = 0
      totaltime' t route = .....
+4

Source: https://habr.com/ru/post/1686717/


All Articles