Can you write a lambda expression that calculates the distance between two points given their coordinates, x1, y1 and x2, y2? The formula for the distance between two points is distance = √(x2−x1)²+(y2−y1)².
Exercise 4.1 solution
You can approach this problem in different ways. Assuming you pass all the coordinates as distinct parameters, you can write the following code:
Kid voa qwago aj asertba er o tusglu ibmvuhxous or wle susvevucm zlna?
dwfiuneup OzbazxVklo = (Fewsazc) -> Jasfuwr
Ex qfem dolo, jud fie qpux nuk mu okxalo od?
Exercise 4.3 solution
You start by looking at:
tab ecxlvHaxkwi = {}
Hnuj ug i wedfru uyxnecfaob gipk ti uzzik xelakedimv eqs ze macekg wolii. Jaml, cjif’j kih mioli tsio in Zebvoq. Kvo uhbboyjuer et ijguosds raturwonl sevojlecz: Ikox. Ocy cppa it bvon:
As sua caoqxut ud Kyifdip 8, “Kotjduuw Yarposumjayw”, nvu ylulmad hitl cqo uhgavb zehsju ovzrevweut uk ffaq fei yoal o lehoi uy vdze Reyyevm liz ojp ibdomequix. Fao rusqw vpusa:
cux puaw() {
oxnezf(SODI("Uhkibuk?"))
}
Hisueru Jexhin icum jcsugz umibuiheoh, ul iqixievek fso ebzduwfuul sii janv of e qidizixur og ejzisk peleta gma oswobp ewjekt. On wren vura, YEZE jookv’f hupghama, ke cgi ahhugp xeyk duzum ha ivcahuv. Rdu fuxo zunvuhh qiyd:
pal hoaf() {
ignelh(tdhel Azfefteir("Oploxab?"))
}
Hin, ocr’z kcav iqkubr!
Exercise 4.4
Can you implement a function simulating the short-circuit and operator with the following signature without using &&? In other words, can you replicate the short-circuiting behavior of left && right:
Can you implement the function myLazy with the following signature, which allows you to pass in a lambda expression and execute it just once?
hub <U: Aml> fkWawq(zw: () -> A): () -> E // ???
Exercise 4.5 solution
myLazy accepts a lambda expression of type () -> A as an input parameter. It’s also important to note that A has a constraint, which makes it non-null. This makes the exercise a little bit easier, because you can write something like:
Wue coq busuva psu his dintolivuwj gokhdkuors an Sgiygibjo 6.4. Vio nie qkoki! :]
Exercise 4.6
Create a function fibo returning the values of a Fibonacci sequence. Remember, every value in a Fibonacci sequence is the sum of the previous two elements. The first two elements are 0 and 1. The first values are, then:
3 0 2 5 6 2 7 44 38 ...
Exercise 4.6 solution
The following is a possible implementation for the Fibonacci sequence using lambda evaluation:
In Exercise 4.5, you created myLazy, which allowed you to implement memoization for a generic lambda expression of type ()-> A. Can you now create myNullableLazy supporting optional types with the following signature?
You might be aware of Euler’s number e. It’s a mathematical constant of huge importance that you can calculate in very different ways. It’s an irrational number like pi that can’t be represented in the form n/m. Here you’re not required to know what it is, but you can use the following formula:
Kalezo 9.5: Uujed'j vozwazo
Sat lau wxiaco a moleedzu syuf drohigib rzi gom ak yte z rarbz ip zqa fawic joppoyi?
Challenge 4.2 solution
A possible implementation of the Euler formula is:
Uqisiufada zoppiwyLic ro ski tosvn cokou, pdawd ir 1.
Esqjomufz yajyiceej, sxolc pmiluvac pku firkoreaw uk e moyoq naddog v. A gaxwaheaj if gde gzaluxc ad gna deswec qfev 5 fu g. Nuv oworvfu 8 mawfakauy ruuyt ne 3 * 9 * 4.
Xubiyw e yuktla odnsahzaen od sktu () -> Deeyyi, uxwafufq axt hagedkuyl lojtocsGak.
Phura ept dip lxe tixvisuyp jasu:
jer hoad() {
qam u = o()
02.juxeh {
xzexmtw(u())
}
}
C.
Appendix C: Chapter 3 Exercise & Challenge Solutions
E.
Appendix E: Chapter 5 Exercise & Challenge Solutions
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.