Skip to content

Instantly share code, notes, and snippets.

@rybarix
Last active March 4, 2020 16:48
Show Gist options
  • Save rybarix/8d1a73b89e740f6b900e50fa83da4069 to your computer and use it in GitHub Desktop.
Save rybarix/8d1a73b89e740f6b900e50fa83da4069 to your computer and use it in GitHub Desktop.
FIIT elm
module Main exposing (main)
import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
main =
text
-- <| String.fromInt(length [1,2,3,4])
-- <| Debug.toString(member 10 [1,2,3])
-- <| Debug.toString(countBigger 10 [1,2,3])
-- <| Debug.toString(listSmaller 1 [1,2,3,4]
-- <| Debug.toString <| afterElem 1 [0,0,1,2,3,4]
-- <| Debug.toString <| zip [1,2,3,1,1,1,1,1,1] ["a","b","c","d"]
-- <| Debug.toString <| map (\a -> a * 5) [1,2,3,4,5]
-- <| fromTuple Debug.toString Debug.toString ("yolo", 3)
<| Debug.toString <| foldL (\acc curr -> acc + curr) 0 [1,2,3,4,5,6,7]
length: List a -> Int
length list =
case list of
[] -> 0
first :: rest ->
1 + length rest
member: a -> List a -> Bool
member x list =
case list of
[]
-> False
first :: rest
-> if first == x then True
else member x rest
countBigger: comparable -> List comparable -> Int
countBigger cmp list =
case list of
[]
-> 0
first::rest
->
if first > cmp then
1 + countBigger cmp rest
else
countBigger cmp rest
{--
const countBigger = (cmp, list) =>
list.length === 0 ?
0 :
countBigger(cmp, list.splice(1)) + 1
--}
listSmaller: comparable -> List comparable -> List comparable
listSmaller c list =
case list of
[] ->
[]
first::rest -> if c < first then
first::listSmaller c rest
else
listSmaller c rest
afterElem: cmp -> List cmp -> List cmp
afterElem c list =
case list of
[] -> []
first :: rest -> if first == c then
rest
else
afterElem c rest
zip: List a -> List b -> List (a, b)
zip lA lB =
case lA of
[] -> []
firstA::restA -> case lB of
[] -> []
firstB::restB -> (firstA,firstB)::zip restA restB
map: (a -> b) -> List a -> List b
map fun list =
case list of
[] -> []
first::rest -> fun first :: map fun rest
{--
function zip(lA, lB) {
switch (lA) {
case []
}
}
--}
fromTuple: (a->String) -> (b->String) -> (a,b) -> String
fromTuple f1 f2 (a,b) =
"(" ++ f1 a ++ "," ++ f2 b ++ ")"
-- funkcia s dvomi args, acc a zoznam
foldL: (a -> b -> b) -> b -> List a -> b
foldL fun acc list =
case list of
[] -> acc
first::rest ->
foldL fun (fun first acc) rest
import Html exposing (text)
main =
-- text <| Debug.toString <| convIfEven [1,2,3,4,5,6]
-- text <| Debug.toString <| between 0 10 [1,2,3,4,100,200,300]
-- text <| Debug.toString <|
-- text <| Debug.toString <| insert 100 (Value 10 (Value 5 Empty Empty) (Value 15 Empty Empty))
-- text <| Debug.toString <| bvsFromList [1,2,3,1000]
text <| Debug.toString <| preorder (bvsFromList [1,2,3,1000])
-- funkcia s dvomi args, acc a zoznam
foldL: (a -> b -> b) -> b -> List a -> b
foldL fun acc list =
case list of
[] -> acc
first::rest ->
foldL fun (fun first acc) rest
foldR: (a -> b -> b) -> b -> List a -> b
foldR fun acc list =
case list of
[] -> acc
first::rest ->
fun first (foldR fun acc rest)
-- returns list of tuple(s)
convIfEven: List Int -> List (Int, Bool)
convIfEven list =
case list of
[] -> []
f::r ->
(f, modBy f 2 == 0) :: convIfEven r
between: number -> number -> List number -> List number
between min max list =
if min > max then []
else
case list of
[] -> []
first::rest ->
if first > min && first < max then first :: between min max rest
else between min max rest
type BVS number = Empty
| Value number (BVS number) (BVS number)
-- type BinTree node = Empty
-- | Value number (BinTree node) (BinTree node)
depth: BVS number -> Int
depth bvs =
case bvs of
Empty -> 0
Value number l r -> 1 + max (depth l) (depth r)
inBVS: (BVS number) -> number -> Bool
inBVS bvs num =
case bvs of
Empty -> False
Value number l r ->
if num < number then inBVS l num
else if num > number then inBVS r num
else
True
-- if number == number then True
-- else inBVS l
insert: number -> BVS number -> BVS number
insert num bvs =
case bvs of
Empty -> (Value num Empty Empty)
Value number l r ->
if number > num then Value number (insert num l) r
else if number < num then Value number l (insert num r)
else bvs
bvsFromList: List number -> BVS number
bvsFromList list =
case list of
[] -> Empty
f::r -> insert f (bvsFromList r)
bvsFromList2: List number -> BVS number
bvsFromList2 list = List.foldr insert Empty list
preorder: BVS number -> List number
preorder bvs =
case bvs of
Empty -> []
Value v l r
-> (preorder l) ++ [v] ++ (preorder r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment