diff --git a/1.hs b/1.hs new file mode 100644 index 0000000..6fd36d8 --- /dev/null +++ b/1.hs @@ -0,0 +1,7 @@ +-- Find the last element of a list. + +myLast :: [a] -> a +myLast [x] = x +myLast (_:xs) = myLast xs + +main = print (myLast [1..10]) diff --git a/2.hs b/2.hs new file mode 100644 index 0000000..e0e5e03 --- /dev/null +++ b/2.hs @@ -0,0 +1,7 @@ +-- Find the last but one element of a list. + +myButLast :: [a] -> a +myButLast [x,y] = x +myButLast (_:xs) = myButLast xs + +main = print (myButLast [1..10]) diff --git a/3.hs b/3.hs new file mode 100644 index 0000000..0fcf274 --- /dev/null +++ b/3.hs @@ -0,0 +1,7 @@ +-- Find the K'th element of a list. The first element in the list is number 1. + +elementAt :: [a] -> Int -> a +elementAt (x:_) 1 = x +elementAt (_:xs) y = elementAt xs (y - 1) + +main = print (elementAt "Haskell" 5) diff --git a/4.hs b/4.hs new file mode 100644 index 0000000..f1a2ef9 --- /dev/null +++ b/4.hs @@ -0,0 +1 @@ +-- Find the number of elements of a list. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bb119cc --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +.PHONY: all clean + +all: clean + +clean: + find . -type f -not -name '*.hs' -not -name 'Makefile' -not -path './.git/*' | xargs rm -f