-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package BuildList where | ||
|
||
import List | ||
|
||
-- A typeclass used to implement a vector construction function which can take | ||
-- any number of arguments (>0). | ||
-- The type parameter `a` is the type of the elements in the list. | ||
-- The type parameter `r` is the return type of the function, which can be a | ||
-- list (base case) or a function (recursive case) that takes another element | ||
-- and returns a new function. | ||
-- The list here is built in reverse for efficiency, and then reversed. | ||
class BuildList a r | r -> a where | ||
lst' :: List a -> a -> r | ||
|
||
instance BuildList a (List a) where | ||
lst' l x = reverse $ x :> l | ||
|
||
instance (BuildList a r) => BuildList a (a -> r) where | ||
lst' l x y = lst' (x :> l) y | ||
|
||
-- Example usage: | ||
-- lst 1 2 3 4 5 => 1 :> 2 :> 3 :> 4 :> 5 :> nil | ||
-- lst False => False :> nil | ||
lst :: (BuildList a r) => a -> r | ||
lst x = lst' nil x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters