move and change

This commit is contained in:
nora 2021-07-08 16:46:51 +02:00
parent 80aa1a2be7
commit 1330cb05e9
10 changed files with 161 additions and 142 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
build
*.ibc

View file

@ -1,17 +0,0 @@
module Palindrome
palindrome : String -> Bool
palindrome str = let halfs = splitHalf str in
fst halfs == (reverse (snd halfs))
where
halfLength : String -> Nat
halfLength str = (length str) `div` 2
splitHalf : String -> (String, String)
splitHalf str = let firstHalf = substr 0 (halfLength str) str
startIndex = if even (length str) then halfLength str else halfLength str + 1
secondHalf = substr startIndex (length str) str in
(firstHalf, secondHalf)
where
even : Nat -> Bool
even n = n `mod` 2 == 0

View file

@ -1,17 +1,17 @@
module Average module Average
import Data.String import Data.String
||| Calculate the average word length of a string ||| Calculate the average word length of a string
||| @str a string containing words seperated by whitespace ||| @str a string containing words seperated by whitespace
export export
average : (str : String) -> Double average : (str : String) -> Double
average str = let totalLength = sum (wordLengths str) average str = let totalLength = sum (wordLengths str)
wordAmount = wordCount str in wordAmount = wordCount str in
cast totalLength / cast wordAmount cast totalLength / cast wordAmount
where where
wordCount : String -> Nat wordCount : String -> Nat
wordCount str = length (words str) wordCount str = length (words str)
wordLengths : String -> List Nat wordLengths : String -> List Nat
wordLengths str = map length (words str) wordLengths str = map length (words str)

View file

@ -1,8 +1,8 @@
module Main module Main
import Average import Average
showAverage : String -> String showAverage : String -> String
showAverage str = show (average str) showAverage str = show (average str)
main : IO () main : IO ()
main = repl "\n> " showAverage main = repl "\n> " showAverage

View file

@ -1,25 +1,25 @@
module Main module Main
-- import System.REPL oh god my setup is horrible -- import System.REPL oh god my setup is horrible
import Data.String import Data.String
average : String -> Double average : String -> Double
average str = let numWords = wordCount str average str = let numWords = wordCount str
totalLength = sum (allLengths (words str)) in totalLength = sum (allLengths (words str)) in
cast totalLength / cast numWords cast totalLength / cast numWords
where where
wordCount : String -> Nat wordCount : String -> Nat
wordCount str = length (words str) wordCount str = length (words str)
allLengths : List String -> List Nat allLengths : List String -> List Nat
allLengths strs = map length strs allLengths strs = map length strs
showAverage : String -> String showAverage : String -> String
showAverage str = "The average word length is: " ++ show (average str) ++ "\n" showAverage str = "The average word length is: " ++ show (average str) ++ "\n"
main : IO () main : IO ()
main = repl "Enter a string: " showAverage main = repl "Enter a string: " showAverage
doubleSum : List Nat -> List Nat doubleSum : List Nat -> List Nat
doubleSum xs = let sum = sum xs in doubleSum xs = let sum = sum xs in
[sum, sum] [sum, sum]

View file

@ -1,2 +1,2 @@
totalLength : List String -> Nat totalLength : List String -> Nat
totalLength xs = sum (map length xs) totalLength xs = sum (map length xs)

34
chapter2/Exercises.idr Normal file
View file

@ -0,0 +1,34 @@
module Exercises
-- 1, 2, 3, 4, 5
palindrome : Nat -> String -> Bool
palindrome n str = let halfs = splitHalf (toLower str) in
length str > n && fst halfs == (reverse (snd halfs))
where
halfLength : String -> Nat
halfLength str = (length str) `div` 2
splitHalf : String -> (String, String)
splitHalf str = let firstHalf = substr 0 (halfLength str) str
startIndex = if even (length str) then halfLength str else halfLength str + 1
secondHalf = substr startIndex (length str) str in
(firstHalf, secondHalf)
where
even : Nat -> Bool
even n = n `mod` 2 == 0
-- 6
counts : String -> (Nat, Nat)
counts str = (length (words str), length str)
-- 7
top_ten : Ord a => List a -> List a
top_ten xs = take 10 (reverse (sort xs))
-- 8
over_length : Nat -> List String -> Nat
over_length n xs = let filtered = filter (\str => length str > n) xs in
length filtered
-- 9
-- not now

View file

@ -1,43 +1,43 @@
double : Num ty => ty -> ty double : Num ty => ty -> ty
double x = x + x double x = x + x
add : Int -> Int -> Int add : Int -> Int -> Int
add x y = x + y add x y = x + y
identity : ty -> ty identity : ty -> ty
identity x = x identity x = x
-- the -- the
der : (ty : Type) -> ty -> ty der : (ty : Type) -> ty -> ty
der ty x = x der ty x = x
||| Higher-Order-Functions ||| Higher-Order-Functions
twice : (a -> a) -> a -> a twice : (a -> a) -> a -> a
twice f x = f (f x) twice f x = f (f x)
Shape : Type Shape : Type
rotate : Shape -> Shape rotate : Shape -> Shape
quadruple : Num a => a -> a quadruple : Num a => a -> a
quadruple = twice double quadruple = twice double
turn_around : Shape -> Shape turn_around : Shape -> Shape
turn_around = twice rotate turn_around = twice rotate
apply_n : (a -> a) -> Nat -> a -> a apply_n : (a -> a) -> Nat -> a -> a
longer : String -> String -> Nat longer : String -> String -> Nat
longer word1 word2 = longer word1 word2 =
let len1 = length word1 let len1 = length word1
len2 = length word2 in len2 = length word2 in
max len1 len2 max len1 len2
pythagoras : Double -> Double -> Double pythagoras : Double -> Double -> Double
pythagoras x y = sqrt (square x + square y) pythagoras x y = sqrt (square x + square y)
where where
square : Double -> Double square : Double -> Double
square x = x * x square x = x * x

View file

@ -1,26 +1,26 @@
module Main module Main
main : IO () main : IO ()
main = putStrLn "hello world" main = putStrLn "hello world"
StringOrInt : Bool -> Type StringOrInt : Bool -> Type
StringOrInt x = case x of StringOrInt x = case x of
True => Int True => Int
False => String False => String
getStringOrInt : (x : Bool) -> StringOrInt x getStringOrInt : (x : Bool) -> StringOrInt x
getStringOrInt x = case x of getStringOrInt x = case x of
True => 94 True => 94
False => "Ninety four" False => "Ninety four"
TypeChooser : String -> Type TypeChooser : String -> Type
TypeChooser x = case x of TypeChooser x = case x of
"Int" => Int "Int" => Int
"Bool" => Bool "Bool" => Bool
"String" => String "String" => String
"Char" => Char "Char" => Char
valToString : (typeChoice : Bool) -> StringOrInt typeChoice -> String valToString : (typeChoice : Bool) -> StringOrInt typeChoice -> String
valToString typeChoice val = case typeChoice of valToString typeChoice val = case typeChoice of
True => cast val -- True means that our argument type is Int, so cast True => cast val -- True means that our argument type is Int, so cast
False => val -- False means that our agumet type is String, so no need to cast False => val -- False means that our agumet type is String, so no need to cast

View file

@ -1,4 +1,4 @@
module Main module Main
main : IO () main : IO ()
main = repl "\n> " reverse main = repl "\n> " reverse