Skip to content

Commit

Permalink
Tail recursive length on cons list
Browse files Browse the repository at this point in the history
  • Loading branch information
vidsinghal committed Feb 4, 2024
1 parent 2b95a5a commit 1e19aeb
Show file tree
Hide file tree
Showing 6 changed files with 1,465 additions and 0 deletions.
27 changes: 27 additions & 0 deletions gibbon-compiler/examples/tail_recursion/ConsListLength.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ConsListLength where


data ConsIntList = Cons Int (ConsIntList) | Nil



mkConsIntList :: Int -> ConsIntList
mkConsIntList len = if len <= 0
then Nil
else let
rst = mkConsIntList (len-1)
in Cons len rst


length :: ConsIntList -> Int -> Int
length lst accum = case lst of
Nil -> accum
Cons x rst -> length rst (accum+1)



gibbon_main =
let n = sizeParam
lst = mkConsIntList n
len = iterate (length lst 0)
in len
Loading

0 comments on commit 1e19aeb

Please sign in to comment.