Skip to content

Instantly share code, notes, and snippets.

Avatar

Marty Stumpf thealmarty

View GitHub Profile
@thealmarty
thealmarty / fact.hs
Created Jan 24, 2019
The factoria function using hylomorphism in Haskell.
View fact.hs
import Data.List --To enable unfoldr.
fact n =
foldr
(*) --Function input
1 --Base case
(unfoldr --List input
(\n -> if n==0 then Nothing else Just (n, n-1))
n)
--Print out example results of the fact fn.
@thealmarty
thealmarty / fact.ml
Last active Jan 24, 2019
The factorial function using hylomorphism in OCaml.
View fact.ml
let rec unfold p g b =
if p b then [] else
(match g b with (a, bprime) ->
a :: unfold p g bprime)
let fact n =
List.fold_right
(* The fold's function input is the times function.*)
(fun x y -> x * y)
(* The fold's list input is the result of this unfold. *)
@thealmarty
thealmarty / zip3.ml
Created Jan 1, 2019
zip3 function in OCaml.
View zip3.ml
(* 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.*)
@thealmarty
thealmarty / zip_sum.ml
Created Jan 1, 2019
A modified zip function in OCaml that sums up the two elements of the input lists in the second item of the output tuple.
View zip_sum.ml
(* 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.*)
@thealmarty
thealmarty / zip_repeat_input2.ml
Created Jan 1, 2019
A modified zip function in OCaml that output a list with the second input list's element repeated.
View zip_repeat_input2.ml
(* 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.*)
@thealmarty
thealmarty / zip_no_zero.ml
Created Jan 1, 2019
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.
View zip_no_zero.ml
(* 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.ml
Last active Jan 3, 2019
The zip function in OCaml, defined with unfold.
View zip.ml
(* 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_sum.hs
Created Jan 1, 2019
A modified zip function in Haskell that sums up the two elements of the input lists in the second item of the output tuple.
View zip_sum.hs
--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_repeat_input2.hs
Created Jan 1, 2019
A modified zip function in Haskell that output a list with the second input list's element repeated.
View zip_repeat_input2.hs
--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_no_zero.hs
Last active Jan 15, 2019
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.
View zip_no_zero.hs
--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