Skip to content

Commit bf17a1f

Browse files
committed
Add a BuildList utility
1 parent 4cbd4f5 commit bf17a1f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Libraries/Base3-Misc/BuildList.bs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package BuildList where
2+
3+
import List
4+
5+
-- A typeclass used to implement a vector construction function which can take
6+
-- any number of arguments (>0).
7+
-- The type parameter `a` is the type of the elements in the list.
8+
-- The type parameter `r` is the return type of the function, which can be a
9+
-- list (base case) or a function (recursive case) that takes another element
10+
-- and returns a new function.
11+
-- The list here is built in reverse for efficiency, and then reversed.
12+
class BuildList a r | r -> a where
13+
lst' :: List a -> a -> r
14+
15+
instance BuildList a (List a) where
16+
lst' l x = reverse $ x :> l
17+
18+
instance (BuildList a r) => BuildList a (a -> r) where
19+
lst' l x y = lst' (x :> l) y
20+
21+
-- Example usage:
22+
-- lst 1 2 3 4 5 => 1 :> 2 :> 3 :> 4 :> 5 :> nil
23+
-- lst False => False :> nil
24+
lst :: (BuildList a r) => a -> r
25+
lst x = lst' nil x

src/Libraries/Base3-Misc/Misc.bsv

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package Misc;
33
import Arbiter::*;
44
import BRAM::*;
55
import BRAMFIFO::*;
6+
import BuildList::*;
67
import BuildVector::*;
78
import BUtils::*;
89
import BypassReg::*;

0 commit comments

Comments
 (0)