Skip to content

Commit

Permalink
refactor(stdlib): Refactor Elara Stdlib and add list length function
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed May 31, 2024
1 parent 85fcf5a commit 2fa03be
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 63 deletions.
4 changes: 0 additions & 4 deletions input.txt

This file was deleted.

2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test:

# Run the project with ghcid auto-recompile
run:
stack build --file-watch --fast --ghc-options='-O0 -fbyte-code' --exec "elara --dump-shunted --dump-core --dump-typed --run"
stack build --file-watch --fast --ghc-options='-O1 -fbyte-code' --exec "elara --dump-shunted --dump-core --dump-typed --run"

# Start Hoogle server for project dependencies
docs:
Expand Down
52 changes: 51 additions & 1 deletion list.elr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,54 @@ def isEmpty : [a] -> Bool
let isEmpty l =
match l with
[] -> True
(x::xs) -> False
(x::xs) -> False

def length : [a] -> Int
let length l =
match l with
[] -> 0
(x::xs) -> 1 + length xs

def sum : [Int] -> Int
let sum a =
match a with
[] -> 0
(x::xs) -> x + sum xs

def reverse : [a] -> [a]
let reverse a =
match a with
[] -> []
(x::xs) -> reverse xs <> [x]

def head : [a] -> a
let head a =
match a with
[] -> undefined
(x::xs) -> x

def last : [a] -> a
let last a =
match a with
[] -> undefined
(x::xs) -> if xs == [] then x else last xs


def map : (a -> b) -> [a] -> [b]
let map f a =
match a with
[] -> []
(x::xs) -> f x :: map f xs

def filter : (a -> Bool) -> [a] -> [a]
let filter f a =
match a with
[] -> []
(x::xs) -> if f x then x :: filter f xs else filter f xs

infixr 5 <>
def (<>) : [a] -> [a] -> [a]
let (<>) a b =
match a with
[] -> b
(x::xs) -> x :: (xs <> b)
51 changes: 1 addition & 50 deletions prelude.elr
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,13 @@ let charToInt c =
else if c == '9' then 9
else -1

infixr 5 <>
def (<>) : [a] -> [a] -> [a]
let (<>) a b =
match a with
[] -> b
(x::xs) -> x :: (xs <> b)


infixr 5 ++
def (++) : String -> String -> String
let (++) a b = listToString ((stringToList a) <> (stringToList b))


def reverse : [a] -> [a]
let reverse a =
match a with
[] -> []
(x::xs) -> reverse xs <> [x]


def head : [a] -> a
let head a =
match a with
[] -> undefined
(x::xs) -> x

def last : [a] -> a
let last a =
match a with
[] -> undefined
(x::xs) -> if xs == [] then x else last xs


def map : (a -> b) -> [a] -> [b]
let map f a =
match a with
[] -> []
(x::xs) -> f x :: map f xs

def filter : (a -> Bool) -> [a] -> [a]
let filter f a =
match a with
[] -> []
(x::xs) -> if f x then x :: filter f xs else filter f xs

def not : Bool -> Bool
let not a =
if a then False else True
Expand All @@ -95,18 +58,6 @@ let (/=) a b =
not (a == b)


def sum : [Int] -> Int
let sum a =
match a with
[] -> 0
(x::xs) -> x + sum xs

def isEmpty : [a] -> Bool
let isEmpty a =
match a with
[] -> True
(x::xs) -> False

def listToString : [Char] -> String
let listToString a =
match a with
Expand Down
8 changes: 1 addition & 7 deletions source.elr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,5 @@ import Elara.Prim
def l : List Int
let l = [1, 2, 3]

def map : (a -> b) -> List a -> List b
let map f l =
match l with
Nil -> Nil
Cons x xs -> Cons (f x) (map f xs)

let main =
print (stringToList "hello")
print (stringToList "hello" |> map identity |> length)

0 comments on commit 2fa03be

Please sign in to comment.