--Problem (*) Find the last element of a list.--(Note, the Lisp transcription of this problem is incorrect.) --Example in haskell:--prelude> mylast [1,2,3,4]--4--prelude> mylast [' x ', ' y ', ' z ']--' z ' module Main wheremain::i O () main = putStrLn "Hello World" Mylast:: [a]->amylast [] = error "Error" Mylast [x] = Xmylast (_:xs) = Mylast Xsmylast ' :: [A]-amylast ' = Head.reversemylast ':: [A]--amylast ' xs = xs!! (length Xs-1)--2 problem 2--(*) Find the last and one element of a list.--(Note that the Lisp transcription of this PR Oblem is incorrect.) --Example in haskell:--prelude> mybutlast [1,2,3,4]--3--prelude> mybutlast [' A ' ... ' Z ']--' y ' mybutlast::[a]-amybutlast [] = error "Error" Mybutlast [x] = Error "error" Mybutlast (x:xs) = if length X s = = 1 then x else mybutlast xsmybutlast ':: [a]->amybutlast ' = Last.initmybutlast ':: [a]->amybutlast ' [x,_] = Xmybutlast "(_:xs) = Mybutlast ' xs--3 problem 3--(*) Find the K ' th element of a list. The first element in the list was number 1.--example:--* (Element-at ' (a b c D e) 3)--c--Example in haskell:--prelude& Gt ElementAt [2--] 2--prelude> elementat "Haskell" 5--' E ' elementat:: [A]->int->aelementat (x:_) 1 = Xel Ementat (_:xs) n = elementat xs (n-1) ElementAt _ _ = Error "Error" ElementAt ':: [A]->int->aelementat ' (x:_) 1 = Xele Mentat ' [] _ = Error "Error" ElementAt ' (_:xs) n = if N<1 then error "error" Else ElementAt ' xs (n-1)--4 proble M 4--(*) Find the number of elements of a list.--Example in haskell:--prelude> mylength [123, 456, 789]--3--Prelud E> mylength "Hello, world!" --13myLength:: [a]->intmylength [] = 0myLength (_:xs) = 1+mylength xsmylength ':: [a], intmylength ' = Sum. Map (\_->1)--5 Problem 5--(*) Reverse a list.--Example in haskell:--prelude> myreverse "A man, a plan, a canal, panama! " --"!amanap, Lanac A, Nalp A, nam a"--prelude> Myreverse [1,2,3,4]--[4,3,2,1]myreverse]:: [A]->[a]myreverse [] = []myreverse (x:xs) = myreverse xs + + + [x]
Clam Squires resistant to hearing