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

View file

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

View file

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

View file

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

View file

@ -1,26 +1,26 @@
module Main
main : IO ()
main = putStrLn "hello world"
StringOrInt : Bool -> Type
StringOrInt x = case x of
True => Int
False => String
getStringOrInt : (x : Bool) -> StringOrInt x
getStringOrInt x = case x of
True => 94
False => "Ninety four"
TypeChooser : String -> Type
TypeChooser x = case x of
"Int" => Int
"Bool" => Bool
"String" => String
"Char" => Char
valToString : (typeChoice : Bool) -> StringOrInt typeChoice -> String
valToString typeChoice val = case typeChoice of
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
module Main
main : IO ()
main = putStrLn "hello world"
StringOrInt : Bool -> Type
StringOrInt x = case x of
True => Int
False => String
getStringOrInt : (x : Bool) -> StringOrInt x
getStringOrInt x = case x of
True => 94
False => "Ninety four"
TypeChooser : String -> Type
TypeChooser x = case x of
"Int" => Int
"Bool" => Bool
"String" => String
"Char" => Char
valToString : (typeChoice : Bool) -> StringOrInt typeChoice -> String
valToString typeChoice val = case typeChoice of
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

View file

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