Fandom

Distributed Interactive Functional Fiction Programming

Map

13pages on
this wiki
Add New Page
Talk0 Share

Main_Page/Snack Code/Functions as arguments

map - apply function to all elements of a list

Haskell:

map':: (a -> b) -> [a] -> [b]
map' _ [] = []
map' f (x:xs) = f x : (map' f xs)

-- Dbl - double argument
dbl:: Int -> Int
dbl x = 2 * x

-- Example of using map':
testMap = map' dbl [1, 2, 3]
-- returns [2,4,6]

Scheme:

(define (map f ls)
  (cond ((null? ls) '())
	(else (cons (f (car ls)) (map f (cdr ls))))
	))

; dbl - double argument
(define (dbl x) (* 2 x))

; Example of using map
(define testMap (map dbl '(1 2 3)))

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.