Skip to content

Instantly share code, notes, and snippets.

View thealmarty's full-sized avatar

Marty Stumpf thealmarty

View GitHub Profile
@thealmarty
thealmarty / unfold.ml
Last active December 13, 2018 17:54
unfold function in OCaml, with a use example.
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)));;
@thealmarty
thealmarty / example.hs
Last active December 13, 2018 17:48
Anamorphisms example in Haskell
--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)
@thealmarty
thealmarty / iterate.ml
Last active December 18, 2018 20:08
Iterate function using unfold in OCaml
(* 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),
@thealmarty
thealmarty / zip8.hs
Last active January 15, 2019 19:16
The zip8 function, a function that zips 8 lists at once, in Haskell.
-- | 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
@thealmarty
thealmarty / zip_no_zero.hs
Last active January 15, 2019 19:16
A zip function in Haskell that stops zipping when any of the input lists' first element is zero, or when any of the input list is empty.
--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
@thealmarty
thealmarty / zip_repeat_input2.hs
Created January 1, 2019 18:57
A modified zip function in Haskell that output a list with the second input list's element repeated.
--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])
@thealmarty
thealmarty / zip_sum.hs
Created January 1, 2019 19:14
A modified zip function in Haskell that sums up the two elements of the input lists in the second item of the output tuple.
--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])
@thealmarty
thealmarty / zip.ml
Last active January 3, 2019 19:57
The zip function in OCaml, defined with unfold.
(* 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.*)
@thealmarty
thealmarty / zip_no_zero.ml
Created January 1, 2019 22:45
A zip function in OCaml that stops zipping when any of the input lists' first element is zero, or when any of the input list is empty.
(* 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.*)
@thealmarty
thealmarty / zip_repeat_input2.ml
Created January 1, 2019 23:06
A modified zip function in OCaml that output a list with the second input list's element repeated.
(* 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.*)