Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
stanfordlambda.gitlab.io
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
3
Issues
3
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
stanfordlambda
stanfordlambda.gitlab.io
Commits
863d0ee8
Commit
863d0ee8
authored
Jan 23, 2019
by
Adithya C. Ganesh
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix indentation
parent
b94e1eb5
Pipeline
#44414835
passed with stages
in 5 minutes and 25 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Sidebyside
Showing
1 changed file
with
37 additions
and
37 deletions
+37
37
assign1.markdown
solutions/assign1.markdown
+37
37
No files found.
solutions/assign1.markdown
View file @
863d0ee8
...
...
@@ 8,63 +8,63 @@ Pick three of the following.
1.
Implement the
`map`
function using a fold.
```
haskell
map'
f
=
foldr
(
\
x
xs
>
f
x
:
xs
)
[]
```
```haskell
map' f = foldr (\ x xs > f x : xs) []
```
2.
Implement the
`filter`
function using a fold.
```
haskell
filter'
p
xs
=
foldr
step
[]
xs
where
step
x
ys

p
x
=
x
:
ys

otherwise
=
ys
```
```haskell
filter' p xs = foldr step [] xs
where step x ys  p x = x : ys
 otherwise = ys
```
3.
Implement
`foldl`
using
`foldr`
.
```
haskell
foldl'
::
(
a
>
b
>
a
)
>
a
>
[
b
]
>
a
```haskell
foldl' :: (a > b > a) > a > [b] > a
foldl'
f
z
xs
=
foldr
step
id
xs
z
where
step
x
g
a
=
g
(
f
a
x
)
```
foldl' f z xs = foldr step id xs z
where step x g a = g (f a x)
```
4.
Write code to compute the smallest positive number that is evenly divisible
by all the numbers from 1 to $n$. Provide an answer for $n = 20$.
```
haskell
myGCD
::
Integral
a
=>
a
>
a
>
a
myGCD
x
0
=
x
myGCD
x
y
=
myGCD
y
(
x
`
mod
`
y
)
```haskell
myGCD :: Integral a => a > a > a
myGCD x 0 = x
myGCD x y = myGCD y (x `mod` y)
myLCM
::
Integral
a
=>
a
>
a
>
a
myLCM
x
y
=
(
x
*
y
)
`
div
`
(
myGCD
x
y
)
myLCM :: Integral a => a > a > a
myLCM x y = (x * y) `div` (myGCD x y)
main
=
putStrLn
.
show
$
n
where
n
=
foldl
myLCM
1
[
1
..
20
]
```
main = putStrLn . show $ n
where
n = foldl myLCM 1 [1..20]
```
`Output: 232792560.`
`Output: 232792560.`
5.
Write code to compute the $n$th prime number. Provide an answer for $n = 10001$.
```
haskell
 See https://wiki.haskell.org/Prime_numbers
 for several optimized implementations.
```haskell
 See https://wiki.haskell.org/Prime_numbers
 for several optimized implementations.
primesTo
m
=
sieve
[
2
..
m
]
where
sieve
(
p
:
xs
)

p
*
p
>
m
=
p
:
xs

True
=
p
:
sieve
[
x

x
<
xs
,
rem
x
p
>
0
]
primesTo m = sieve [2..m]
where
sieve (p:xs)
 p*p > m = p : xs
 True = p : sieve [x  x < xs, rem x p > 0]
main
=
putStrLn
.
show
$
(
ps
!!
10000
)
where
ps
=
primesTo
1000000
```
main = putStrLn . show $ (ps !! 10000)
where
ps = primesTo 1000000
```
`Output: 104743.`
`Output: 104743.`
## References
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment