Skip to content

Instantly share code, notes, and snippets.

@ggb
Last active May 17, 2016 11:17
Show Gist options
  • Save ggb/a29f256e005cf2f309ad to your computer and use it in GitHub Desktop.
Save ggb/a29f256e005cf2f309ad to your computer and use it in GitHub Desktop.
Elm list (integer list/generic list) implementation for presentation purposes
module MyIntList exposing (..)
type MyList = Nil | Cons Int MyList
(#) : Int -> MyList -> MyList
(#) ele list =
Cons ele list
(##) : MyList -> MyList -> MyList
(##) l1 l2 =
case l1 of
Cons val rest ->
val # (rest ## l2)
Nil ->
l2
head : MyList -> Maybe Int
head list =
case list of
Cons val rest ->
Just val
Nil ->
Nothing
rest : MyList -> Maybe MyList
rest list =
case list of
Cons val rest ->
Just rest
Nil ->
Nothing
-- reduce left
reduce : (Int -> b -> b) -> b -> MyList -> b
reduce fun acc list =
case list of
Cons val rest ->
reduce fun (fun val acc) rest
Nil ->
acc
-- reduce right
reduce' : (Int -> b -> b) -> b -> MyList -> b
reduce' fun acc list =
case list of
Cons val rest ->
fun val (reduce' fun acc rest)
Nil ->
acc
sum : MyList -> Int
sum =
reduce (+) 0
mult : MyList -> Int
mult =
reduce (*) 1
length : MyList -> Int
length list =
reduce (\ele acc -> acc + 1) 0 list
map : (Int -> Int) -> MyList -> MyList
map fun list =
reduce' (\ele acc -> (fun ele) # acc) Nil list
filter : (Int -> Bool) -> MyList -> MyList
filter pred list =
reduce' (\ele acc -> if pred ele then ele # acc else acc) Nil list
all : (Int -> Bool) -> MyList -> Bool
all pred list =
reduce' (\ele acc -> pred ele && acc) True list
any : (Int -> Bool) -> MyList -> Bool
any pred list =
reduce' (\ele acc -> pred ele || acc) False list
contains : Int -> MyList -> Bool
contains q list =
case list of
Cons val rest ->
if val == q then
True
else
contains q rest
Nil ->
False
indexOf : Int -> MyList -> Int
indexOf q list =
let helper l =
case l of
Cons val rest ->
if val == q then
1
else
1 + indexOf q rest
Nil ->
0
in
if contains q list then
helper list
else
-1
get : Int -> MyList -> Maybe Int
get index list =
let helper i l =
case l of
Cons val rest ->
if i == index then
Just val
else
helper (i + 1) rest
Nil ->
Nothing
in
helper 0 list
module MyList exposing (..)
type MyList a = Nil | Cons a (MyList a)
(#) : a -> MyList a -> MyList a
(#) ele list =
Cons ele list
(##) : MyList a -> MyList a -> MyList a
(##) l1 l2 =
case l1 of
Cons val rest ->
val # (rest ## l2)
Nil ->
l2
head : MyList a -> Maybe a
head list =
case list of
Cons val rest ->
Just val
Nil ->
Nothing
rest : MyList a -> Maybe (MyList a)
rest list =
case list of
Cons val rest ->
Just rest
Nil ->
Nothing
-- reduce left
reduce : (a -> b -> b) -> b -> MyList a -> b
reduce fun acc list =
case list of
Cons val rest ->
reduce fun (fun val acc) rest
Nil ->
acc
-- reduce right
reduce' : (a -> b -> b) -> b -> MyList a -> b
reduce' fun acc list =
case list of
Cons val rest ->
fun val (reduce' fun acc rest)
Nil ->
acc
sum : MyList Int -> Int
sum =
reduce (+) 0
mult : MyList Int -> Int
mult =
reduce (*) 1
length : MyList a -> Int
length list =
reduce (\ele acc -> acc + 1) 0 list
map : (a -> b) -> MyList a -> MyList b
map fun list =
reduce' (\ele acc -> (fun ele) # acc) Nil list
filter : (a -> Bool) -> MyList a -> MyList a
filter pred list =
reduce' (\ele acc -> if pred ele then ele # acc else acc) Nil list
all : (a -> Bool) -> MyList a -> Bool
all pred list =
reduce' (\ele acc -> pred ele && acc) True list
any : (a -> Bool) -> MyList a -> Bool
any pred list =
reduce' (\ele acc -> pred ele || acc) False list
contains : a -> MyList a -> Bool
contains q list =
case list of
Cons val rest ->
if val == q then
True
else
contains q rest
Nil ->
False
indexOf : a -> MyList a -> Int
indexOf q list =
let helper l =
case l of
Cons val rest ->
if val == q then
1
else
1 + indexOf q rest
Nil ->
0
in
if contains q list then
helper list
else
-1
get : Int -> MyList a -> Maybe a
get index list =
let helper i l =
case l of
Cons val rest ->
if i == index then
Just val
else
helper (i + 1) rest
Nil ->
Nothing
in
helper 0 list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment