jinkou2 jinkou2
jinkou2 jinkou2

Reputation: 1096

other codes for this recursive function on list

i wrote a function to find the position of the next column of an asciibox
for instance :

+-----++---++---+
|  a  || b || c |
+-----++---++---+

box b should start at column "pseudo code " {length (box a)} and box c should start at pseudo code {length box a + length box b}

so i wrote this function

f:: [Int]->[Int]->[Int]
f (x:xs) []  = f xs [x]
f  [] ys = ys
f (x:xs) ys = f xs (ys++[x+ last ys])

which gives me what i want

f [23,24,25] [] 
ghci> [23,47,72]

i would like to know if other algorithm is possible . Is there any solution with "fold" or "unfold" or perhaps "iterate"

thanks

Upvotes: 1

Views: 100

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 153247

> scanl (+) 0 [23, 24, 25]
[0,23,47,72]

Upvotes: 5

Related Questions