Question: Computer Science ConnectionsCurryingFor a binary predicate P ( x , y ) in an expression like 8 y : 8 x : P ( x

Computer Science ConnectionsCurryingFor a binary predicate P(x, y) in an expression like 8y : 8x : P(x, y), we can think of this expression as frst pluggingin a value for y, which then yields a unary predicate 8x : P(x, y)(which takes an argument x). Theres an interestingparallel between this view of nested quantifers and a way of writing functions in some programming languages.1(* M L *)2 fun sum a b = a + b;A quick note on ML syntax:fun is a keyword that sayswere defning a function; sumis the name of it; a b is thelist of arguments; and thatfunction is defned to returnthe value of a+b.3 sum 32; (* returns 5*)4 sum 9912; (* returns 111*)1 # Python2 def sum(a,b):3 return a+b4 sum(3,2) # returns 55 sum(99,12) # returns 1111 ; Scheme2(define sum3(lambda (a b)(+ a b)))For Scheme: (lambda argsbody) denotes the functionthat takes arguments args andreturns the value of thefunction body body. Also,Scheme is a prefx language,so applying the function f toarguments arg1, arg2,,argN is written (f arg1 arg2... argN); for example, (+12) has the value 3.4(sum 32) ;; returns 55(sum 9912) ;; returns 111Figure 3.30 A sum function, implemented in three languages.For concreteness, lets think about a smallfunction that takes two arguments and justreturns their sum. Figure 3.30 shows implementations of this function in three differentprogramming languages, and then uses sumto actually compute 3+2 and 99+12.But now suppose that we want to defne afunction that takes one argument and adds 3to it. Can we make use of the sum functionto do so?(The analogy to predicates is thattaking a two-argument predicate and applying it to one argument gives one-argumentpredicate; here were trying to take a twoargument function in a programming language and apply it to one argument to yield aone-argument function.) The answer is yesand it turns out that creating the add 3function using sum is very easy in ML: we simply apply sum to one argument, and the result is a function thatstill wants one more argument. To do this, we execute val add3= sum 3; which defnes a value (thats the valpart of the syntax) whose name is add3 and whose value is sum applied to 3. That makes add3 a one-argumentfunction, and now add30 has the value 3; add3108 has the value 111; and add3199 has the value 202.1 fun sum a b = a + b;2 val add3= sum 3;34 sum 32; (* returns 5*)5 add32; (* returns 5*)1 def sum(a):2 def sumA(b):3 return a+b4 return sumA56 add3= sum(3)78 sum(3)(2) # returns 59 add3(2) # returns 51(define sum (lambda (a)2(lambda (b)(+ a b)))Defne sum to be a functionthat takes an argument a andreturns ...... the function that takes anargument b and returns a+b.(This function [taking b,returning a+b] is called sumAin the Python code, and isunnamed in the Schemecode.)34(define add3(sum 3))56((sum 3)2) ;; returns 57(add32) ;; returns 5Figure 3.31 Curried implementations of sum.A function like sum in ML, which takes itsmultiple arguments one at a time, is said tobe Curriedafter the logician Haskell Curry(19001982).(As usual, theres a richer history than a concept named after one personwould suggest: the idea dates back at least toMoses Schnfnkel (18881942) and GottlobFrege (18481925), two earlier logicians.)The programming language Haskell is alsonamed in Currys honor. Thinking about Curried functions is a classical topic in the studyof programming languages. (Currying canbe very handy in programmingparticularlywhen you have one parameter to a functionthat stays the same for many different calls.......This is a discrete math class, we have to show computer science is related to this
Computer Science ConnectionsCurryingFor a binary

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!