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
--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 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)));; |
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
(* 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 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
(* Define the unfold function that takes 3 inputs.*) | |
let rec unfold p g b1 b2 b3 = | |
if p b1 b2 b3 then [] else | |
(match g b1 b2 b3 with (a, (b1prime, b2prime, b3prime)) -> | |
a :: unfold p g b1prime b2prime b3prime) | |
;; | |
(* Define the zip3 function.*) | |
let zip3 = unfold | |
(* Define p.*) |
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
(* Define the unfold function.*) | |
let rec unfold p g b1 b2 = | |
if p b1 b2 then [] else | |
(match g b1 b2 with (a, (b1prime, b2prime)) -> | |
a :: unfold p g b1prime b2prime) | |
;; | |
(* Define the zip_sum function.*) | |
let zip_sum = unfold | |
(* Define p.*) |
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
(* Define the unfold function.*) | |
let rec unfold p g b1 b2 = | |
if p b1 b2 then [] else | |
(match g b1 b2 with (a, (b1prime, b2prime)) -> | |
a :: unfold p g b1prime b2prime) | |
;; | |
(* Define the zip_repeat_input2 function.*) | |
let zip_repeat_input2 = unfold | |
(* Define p.*) |
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
(* Define the unfold function.*) | |
let rec unfold p g b1 b2 = | |
if p b1 b2 then [] else | |
(match g b1 b2 with (a, (b1prime, b2prime)) -> | |
a :: unfold p g b1prime b2prime) | |
;; | |
(* Define the zip_no_zero function.*) | |
let zip_no_zero = unfold | |
(* Define p.*) |
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
(* Define the unfold function.*) | |
let rec unfold p g b1 b2 = | |
if p b1 b2 then [] else | |
(match g b1 b2 with (a, (b1prime, b2prime)) -> | |
a :: unfold p g b1prime b2prime) | |
;; | |
(* Define the zip function.*) | |
let zip = unfold | |
(* Define p.*) |
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
--As modified from the Prelude: | |
zip_sum ::(Num a, Eq a)=> [a] -> [a] -> [(a,a)] | |
zip_sum [] _bs = [] | |
zip_sum _as [] = [] | |
zip_sum (a:as) (b:bs) = (a, (a + b) ) : zip_sum as bs | |
main = do | |
print (zip_sum [0,1,2] [1,2,3]) | |
print (zip_sum [1,2,3,4,0,5,6] [1,2,3,4,5,6,0]) |
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
--As modified from the Prelude: | |
zip_repeat_input2 ::(Num a, Eq a, Num b, Eq b)=> [a] -> [b] -> [(a,b,b)] | |
zip_repeat_input2 [] _bs = [] | |
zip_repeat_input2 _as [] = [] | |
zip_repeat_input2 (a:as) (b:bs) = (a,b,b) : zip_repeat_input2 as bs | |
main = do | |
print (zip_repeat_input2 [0,1,2] [1,2,3]) | |
print (zip_repeat_input2 [1,2,3,4,0,5,6] [1,2,3,4,5,6,0]) |
OlderNewer