This file contains hidden or 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
--As modified from the Prelude: | |
zip_no_zero ::(Num a, Eq a, Num b, Eq b)=> [a] -> [b] -> [(a,b)] | |
zip_no_zero [] _bs = [] | |
zip_no_zero _as [] = [] | |
--Add to p: when any of the input lists' first element is zero. | |
zip_no_zero (0:as) _bs = [] | |
zip_no_zero _as (0:bs) = [] | |
zip_no_zero (a:as) (b:bs) = (a,b) : zip_no_zero as bs |
This file contains hidden or 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
-- | The 'zipWith8' function takes a function which combines eight | |
-- elements, as well as eight lists and returns a list of their point-wise | |
-- combination, analogous to 'zipWith'. | |
zipWith8 :: (a->b->c->d->e->f->g->h->i) -> | |
[a]->[b]->[c]->[d]->[e]->[f]->[g]->[h]->[i] | |
zipWith8 z (a:as) (b:bs) (c:cs) (d:ds) (e:es) (f:fs) (g:gs) (h:hs) | |
= z a b c d e f g h : zipWith8 z as bs cs ds es fs gs hs | |
zipWith8 _ _ _ _ _ _ _ _ _ = [] | |
-- | The 'zip8' function takes eight lists and returns a list of |
This file contains hidden or 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
(* The unfold function takes in | |
a predicate (p) and | |
a function (g) which takes an input (b). *) | |
let rec unfold p g b = | |
if p b then [] else | |
(match g b with (a, bprime) -> | |
a :: unfold p g bprime) | |
;; | |
(* The iterate function takes in a function (f), |
This file contains hidden or 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
--Import Data.List so that we can use the unfoldr function. | |
import Data.List | |
--p and g are combined into one function which you input to unfoldr in Haskell: | |
example = unfoldr (\x -> if x > 9 then Nothing else Just (x, x+1)) | |
--Print out results of (example 7) and (example 0). | |
main = do | |
print (example 7) | |
print (example 0) |
This file contains hidden or 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
let rec unfold p g b = | |
if p b then [] else | |
(match g b with (a, bprime) -> | |
a :: unfold p g bprime) | |
;; | |
(*Implement the example: *) | |
let example = unfold (fun x -> (x > 9)) (fun x -> (x, (x + 1)));; |
NewerOlder