mirror of
https://github.com/Noratrieb/idris-learning.git
synced 2026-01-16 05:55:02 +01:00
move and change
This commit is contained in:
parent
80aa1a2be7
commit
1330cb05e9
10 changed files with 161 additions and 142 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
build
|
||||||
|
*.ibc
|
||||||
|
|
@ -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
|
|
||||||
34
chapter2/Exercises.idr
Normal file
34
chapter2/Exercises.idr
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue