mirror of
https://github.com/edwinb/Idris2-boot.git
synced 2025-01-04 10:45:11 +03:00
Fix a stack overflow with a tail recursive list integer length function
Relates to #133 and #135.
This commit is contained in:
parent
fd980bc92f
commit
7e8642e8f8
@ -338,8 +338,19 @@ export
|
|||||||
export
|
export
|
||||||
TTC a => TTC (List a) where
|
TTC a => TTC (List a) where
|
||||||
toBuf b xs
|
toBuf b xs
|
||||||
= do toBuf b (cast {to=Int} (length xs))
|
= do toBuf b (TailRec_length xs)
|
||||||
traverse_ (toBuf b) xs
|
traverse_ (toBuf b) xs
|
||||||
|
where
|
||||||
|
||| Tail-recursive length as buffer sizes can get large
|
||||||
|
|||
|
||||||
|
||| Once we port to Idris2, can use Data.List.TailRec.length instead
|
||||||
|
length_aux : List a -> Int -> Int
|
||||||
|
length_aux [] len = len
|
||||||
|
length_aux (_::xs) len = length_aux xs (1 + len)
|
||||||
|
|
||||||
|
TailRec_length : List a -> Int
|
||||||
|
TailRec_length xs = length_aux xs 0
|
||||||
|
|
||||||
fromBuf b
|
fromBuf b
|
||||||
= do len <- fromBuf b {a = Int}
|
= do len <- fromBuf b {a = Int}
|
||||||
readElems [] (cast len)
|
readElems [] (cast len)
|
||||||
|
Loading…
Reference in New Issue
Block a user